package com.thread.demo;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class FutrueCllasbledemo {
/**
* @param args
* @throws ExecutionException
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService service=Executors.newCachedThreadPool();
ArrayList
for(int i=0;i<5;i++){
arraylist.add(service.submit(new Callback()));
}
for(Future
System.out.println(future.get());
}
service.shutdown();
}
}
class Callback implements Callable
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
return "hello";
}
}
7、关于对线程使用sleep演示,由于当前线程挂起,自然导致线程驱动的任务挂起。以下为演示例子:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadCompeteDemo {
public static void main(String[] a){
ExecutorService service=Executors.newCachedThreadPool();
service.execute(new Threademo4());
service.execute(new Threademo5());
service.shutdown();
}
}
class Threademo4 implements Runnable{
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("liu");
}
}
class Threademo5 implements Runnable{
@Override
public void run() {
System.out.println("yangyang");
}
}
关于一些重要特性的总结:
线程的优先级将该线程的重要性传递给了调度器。
Excutor线程管理器可以对管理器的线程实行一次性的全部关闭。
创建任务,将线程附着到任务上,以使线程可以驱动任务。
在java中,Thread类并不执行任何操作,它只是驱动他的任务。
Java的线程机制基于来自c的低级的p线程方式。
ThreadFactory工厂通常和ExecutorService 联合使用,通常前者位于后者的构造函数中,ThreadFactory主要为ExecutorService线程管理器产生的线程定制某些行为和属性。
作者“690360459-qq-com”