1、線程初始化、中斷
public class ThreadDemo1 extends Thread {
public ThreadDemo1(String name) {
super(name);
}
@Override
public void run() {
// interrupted()是檢測中斷並清除中斷狀態;isInterrupted()只檢測中斷
while(!interrupted()) {
System.out.println(getName() + "線程執行了...");
try {
Thread.sleep(2000);
} catch(InterruptedException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String[] args) {
ThreadDemo1 d1 = new ThreadDemo1("first-thread");
ThreadDemo1 d2 = new ThreadDemo1("second-thread");
// setDaemon(true)設置線程爲守護線程,如果沒有其他用戶線程則結束
// d1.setDaemon(true);
// d2.setDaemon(true);
d1.start();
d2.start();
// interrupt()不會真正終止線程
d1.interrupt();
}
}
2、實現Runnable接口
public class ThreadDemo2 implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("thread running...");
}
}
public static void main(String[] args) {
Thread thread = new Thread(new ThreadDemo2());
thread.start();
}
}
3、實現Callable接口
public class ThreadDemo3 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("正在進行緊張的計算...");
Thread.sleep(2000);
return 1;
}
public static void main(String[] args) throws Exception {
FutureTask<Integer> task = new FutureTask<>(new ThreadDemo3());
Thread thread = new Thread(task);
thread.start();
Integer result = task.get();
System.out.println("線程執行結果爲:" + result);
}
}
4、定時器(常用框架quartz)
public class ThreadDemo4 {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("timertask is run");
}
}, 0, 1000);
}
}
5、線程池
public class ThreadDemo5 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(10);
for(int i = 0; i < 11; i++) {
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
});
}
}
}
6、join
public class ThreadDemo6 {
public void thread1(Thread joinThread) {
System.out.println("線程1執行了...");
joinThread.start();
try {
joinThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程1執行完畢");
}
public void thread2() {
System.out.println("加塞線程開始執行...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("加塞線程執行完畢");
}
public static void main(String[] args) {
ThreadDemo6 threadDemo6 = new ThreadDemo6();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
threadDemo6.thread2();
}
});
new Thread(new Runnable() {
@Override
public void run() {
threadDemo6.thread1(t1);
}
}).start();
}
}
7、ThreadLocal
public class ThreadDemo7 {
private ThreadLocal<Integer> count = new ThreadLocal<Integer>() {
protected Integer initialValue() {
return new Integer(0);
};
};
public int getNext() {
Integer value = count.get();
value++;
count.set(value);
return value;
}
public static void main(String[] args) {
ThreadDemo7 threadDemo7 = new ThreadDemo7();
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
System.out.println(Thread.currentThread().getName() + " " + threadDemo7.getNext());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
System.out.println(Thread.currentThread().getName() + " " + threadDemo7.getNext());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
System.out.println(Thread.currentThread().getName() + " " + threadDemo7.getNext());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}