public class ThreadRunn extends Thread {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("run重写");
}
}
public static void main(String[] args) {
Thread t1 = new ThreadRunn();
t1.start();
for (int i = 0; i < 20; i++) {
System.out.println("main");
}
}
}
public class Runnableee implements Runnable {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("run重写");
}
}
public static void main(String[] args) {
Runnableee r = new Runnableee();
Thread t1 = new Thread(r);
t1.start();
for (int i = 0; i < 20; i++) {
System.out.println("main");
}
}
}
public class NoNameee {
public static void main(String[] args) {
new Thread() {
@Override
public void run() {
System.out.println("...");
}
}.start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("。。。");
}
}).start();
}
}
public class ThreadNameee extends Thread {
//修改默认线程名,需先设置构造方法
public ThreadNameee(String str) {
super(str);
}
@Override
public void run() {
System.out.println(getId() + " " + getName());
setName("xiao");
System.out.println(getId() + " " + getName());
}
public static void main(String[] args) {
ThreadNameee t1 = new ThreadNameee("ying");
t1.start();
//获取主线程的引用
Thread t2 = Thread.currentThread();
System.out.println("主线程的id:" + t2.getId());
}
}
public class ThreadTimee extends Thread {
private static boolean flag = true;
@Override
public void run() {
while (flag) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
System.out.println(sdf.format(date));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new ThreadTimee();
t1.start();
//主线程睡眠5秒后停止
System.out.println("start sleep");
Thread.sleep(5000);
flag = false;
//t1.stop();
System.out.println("stop thread");
}
}
public class ThreadPriorityy extends Thread {
@Override
public void run() {
System.out.println("run:" + getPriority());
}
public static void main(String[] args) {
Thread thread = new ThreadPriorityy();
thread.start();
thread.setPriority(MAX_PRIORITY);
System.out.println("main:" + Thread.currentThread().getPriority());
}
}
public class ThreadJoin extends Thread {
@Override
public void run() {
System.out.println("start");
for (int i = 0; i < 5; i++) {
System.out.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("end");
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new ThreadJoin();
thread.start();
System.out.println("开始等待");
//thread.join();
thread.join(3000);//等待3秒
System.out.println("结束等待");
}
}
//守护线程会随主线程结束,不论是否执行完全
public class ThreadDaemonn extends Thread {
@Override
public void run() {
System.out.println(isDaemon() ? "是守护线程" : "不是守护线程");
for (int i = 0; i < 20; i++) {
System.out.println("子线程:" + i);
}
}
public static void main(String[] args) {
ThreadDaemonn thread = new ThreadDaemonn();
thread.setDaemon(true);
thread.start();
for (int i = 0; i < 20; i++) {
System.out.println("主线程");
}
}
}
public synchronized void run() {
System.out.println("线程启动...");
//synchronized (this) {
...
//}
}
public void run(){
synchronized(a){
synchronized(b){
...
}
}
}
public void run(){
synchronized(b){
synchronized(a){
...
}
}
}