线程的创建和线程池的使用
1、线程的几种创建方式
1.1 继承 Thread 类重写 run 方法
public class ThreadPollExecutorTest {public static void main(String[] args){Thread thread = new MyThread();
// 线程的名字
thread.setName("MyThread001111111");
thread.start();}
static class MyThread extends Thread{
@Override
public void run(){System.out.println(Thread.currentThread().getName()+",running,,,,,,,,,,");
}
}
}
1.2 实现 Runnable 接口
public class RunnablTest {public static void main(String[] args){Thread thread = new Thread(new MyRunnable());
thread.setName("myRunnable0001");
thread.start();}
static class MyRunnable implements Runnable{
@Override
public void run(){System.out.println("my runnable......");
}
}
}
1.3 实现 Callable 接口通过 FutureTask 包装器来创建 Thread 线程
因为该接口可以返回一个结果,所以我们需要获取返回结果,这时系统提供了一个 Future 接口用于获取返回的结果,但是该接口代表的是一个异步计算的结果,所以我们需要等到计算完成时才能获取结果,否则就会一直阻塞直到结果返回。
所以我们获取结果要在调用 isDone()方法返回 true 时才能调用 get()方法。该接口有一个系统提供的实现类 FutureTask,FutureTask 类实现了 RunnableFuture<V>,RunnableFuture<V> 接口继承了 Runnable, Future<V> 两个接口。FutureTask 中实现的 Runnable 中的 run 方法调用了 Callable 接口中的 call 方法。所以创建 Callable 接口的线程的方法与 Runnable 类似:
public class CallableTest {public static void main(String[] args) throws InterruptedException, ExecutionException {FutureTask<String> future = new FutureTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {return "hello";}
});
Thread thread = new Thread(future);
thread.setName("myCallable001");
thread.start();
while (!future.isDone()){
try {System.out.println(thread.getName()+ "is not done");
Thread.currentThread().sleep(1000);
}catch (InterruptedException e){e.printStackTrace();
}
}
String result = future.get();
System.out.println("result:"+result);
}
}
2、线程池的使用
Java 通过 Executors 提供四种线程池,分别为:
newCachedThreadPool 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序 (FIFO, LIFO, 优先级) 执行。
2.1newCachedThreadPool
public static void main(String[] args) {ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final int index = i;
try {Thread.sleep(10);
} catch (InterruptedException e) {e.printStackTrace();
}
cachedThreadPool.execute(new Runnable() {public void run() {System.out.println(index);
}
});
}
}
newFixedThreadPool
public static void main(String[] args) {ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
for (int i = 0; i < 10; i++) {
final int index = i;
fixedThreadPool.execute(new Runnable() {public void run() {
try {System.out.println(index);
Thread.sleep(10);
} catch (InterruptedException e) {e.printStackTrace();
}
}
});
}
}
newScheduledThreadPool
public static void main(String[] args) {ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
for (int i = 0; i < 10; i++) {scheduledThreadPool.schedule(new Runnable() {public void run() {System.out.println("delay 3 seconds");
}
}, 3, TimeUnit.SECONDS);
}
}
newSingleThreadExecitor
public static void main(String[] args) {ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() {public void run() {/* System.out.println(index);*/
try {System.out.println(index);
Thread.sleep(2000);
} catch (InterruptedException e) {e.printStackTrace();
}
}
});
}
}