java启动多线程的4种方法
继承Thread类
public class ThreadTest1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Thread is printing");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ThreadTest1 myThread = new ThreadTest1();
myThread.start();
for (int i = 0; i < 3; i++) {
System.out.println("Main is printing");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行结果:
Main is printing
Thread is printing
Main is printing
Thread is printing
Thread is printing
Main is printing
Thread is printing
Thread is printing
实现Runnable接口
public class ThreadTest2 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Thread is printing");
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ThreadTest2 myThread = new ThreadTest2();
Thread thread = new Thread(myThread);
thread.start();
for (int i = 0; i < 3; i++) {
System.out.println("Main is printing");
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行结果
Main is printing
Thread is printing
Main is printing
Thread is printing
Main is printing
Thread is printing
Thread is printing
Thread is printing
实现Callable接口
这种方法可以返回线程执行的结果。
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadTest3 implements Callable<String> {
@Override
public String call() throws Exception {
for (int i = 0; i < 5; i++) {
System.out.println("Thread is printing");
Thread.currentThread().sleep(1000);
}
return "Done";
}
public static void main(String[] args) {
ThreadTest3 myThread = new ThreadTest3();
FutureTask<String> futureTask = new FutureTask<String>(myThread);
Thread thread = new Thread(futureTask);
thread.start();
for (int i = 0; i < 3; i++) {
System.out.println("Main is printing");
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
System.out.println("Thread return: " + futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
运行结果
Main is printing
Thread is printing
Main is printing
Thread is printing
Main is printing
Thread is printing
Thread is printing
Thread is printing
Thread return: Done
通过线程池获取线程
Java通过Executors提供四种线程池
- CachedThreadPool 创建可缓存的线程池
- FixedThreadPool 创建定长的线程池,超出线程池长度的线程在队列中等待
- ScheduledThreadPool 创建一个定长的线程池,支持定时和周期性任务
- SingleThreadExecutor 创建一个单线程的线程池,所有任务通过指定顺序(FIFO,LIFO,优先级)执行
这里就不详细介绍四种线程池的区别了。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadTest4 {
public static void main(String[] args) {
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
final int j = i;
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
for (int k = 0; k < 2; k++) {
System.out.printf("Thread %d is printing\n", j);
try {
Thread.currentThread().sleep(1000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}
}
运行结果:
Thread 0 is printing
Thread 4 is printing
Thread 3 is printing
Thread 2 is printing
Thread 1 is printing
Thread 1 is printing
Thread 2 is printing
Thread 3 is printing
Thread 4 is printing
Thread 0 is printing