import java.util.List;
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;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class CallableTest implements Callable<String>{
String name="";
numTest t;
static int n=0;
Integer num = 10;
public CallableTest(numTest t) {
super();
this.t = t;
}
public CallableTest() {
super();
}
@Override
public String call(){
// TODO Auto-generated method stub
try {
System.out.println(t.getNum()+"aaaa");
if(t.getNum()>0) {
//num = num-1;
System.out.println(name+" ===="+Thread.currentThread().getName()+" "+t.getNum());
//Thread.sleep(1);
}
System.out.println(t.getNum()+"bbbbbbb");
if(t.getNum()>0) {
/*synchronized (t) {
if(t.getNum()>0) {
int num = t.getNum();
System.out.println(Thread.currentThread().getName()+"当前num是"+num);
num = num-1;
System.out.println(Thread.currentThread().getName()+"剩下"+num);
t.setNum(num);
Thread.sleep(1);
}else {
System.out.println(Thread.currentThread().getName()+"num的值是"+t.getNum());
}
}*/
if(num>0) {
System.out.println(Thread.currentThread().getName()+"当前num是"+num);
num = num-1;
System.out.println(Thread.currentThread().getName()+"剩下"+num);
Thread.sleep(10);
System.out.println("========================="+n++);
}else {
System.out.println(Thread.currentThread().getName()+"num的值是"+num);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Thread.currentThread().getName();
}
public static void main(String[] args) throws Exception{
t2();
}
//变量共享
public static void t() {
numTest numTest = new numTest();
CallableTest test1 = new CallableTest(numTest);
FutureTask<String> task1 = new FutureTask<String>(test1);
new Thread(task1,"线程一").start();
CallableTest test2 = new CallableTest(numTest);
FutureTask<String> task2 = new FutureTask<String>(test1);
new Thread(task2,"线程二").start();
System.out.println(n);
try {
System.out.println(task1.get()+"************");
System.out.println(task2.get()+"************");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//线程池
public static void t1() {
/*CallableTest test1 = new CallableTest();
FutureTask<String> task = new FutureTask<String>(test1);
ExecutorService pool = Executors.newFixedThreadPool(1);
pool.execute(task);
System.out.println(task.get());
pool.submit(task);
System.out.println(task.get());*/
numTest numTest = new numTest();
CallableTest test1 = new CallableTest(numTest);
//test1.name="aaa";
FutureTask<String> task1 = new FutureTask<String>(test1);
CallableTest test2 = new CallableTest(numTest);
//test2.name="bb";
FutureTask<String> task2 = new FutureTask<String>(test2);
CallableTest test3 = new CallableTest(numTest);
//test3.name="ccc";
FutureTask<String> task3 = new FutureTask<String>(test3);
ExecutorService pool = Executors.newFixedThreadPool(2);
pool.submit(task1);
pool.submit(task2);
pool.submit(task3);
/*try {
//System.out.println(task1.get(10,TimeUnit.MILLISECONDS));
System.out.println(task1.get()+"===========================");
System.out.println(task1.isDone());
System.out.println(task1.isCancelled());
//System.out.println(task2.get(1,TimeUnit.MILLISECONDS));
//System.out.println(task2.get(1,TimeUnit.MILLISECONDS));
System.out.println(task2.get()+"===========================");
System.out.println(task2.isDone());
System.out.println(task2.isCancelled());
System.out.println(pool.isShutdown());
System.out.println(pool.isTerminated()+"=====");
System.out.println(task3.get()+"===========================");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
pool.shutdown();
System.out.println(pool.isTerminated()+"===1==");
List<Runnable> shutdownNow = pool.shutdownNow();
for(Runnable r:shutdownNow) {
System.out.println(r.toString());
}
System.out.println(pool.isTerminated()+"===2=="+shutdownNow.size());
}
//线程池
public static void t2() throws Exception{
numTest numTest = new numTest();
CallableTest test1 = new CallableTest();
FutureTask<String> task1 = new FutureTask<String>(test1);
FutureTask<String> task2 = new FutureTask<String>(test1);
FutureTask<String> task3 = new FutureTask<String>(test1);
ExecutorService pool = Executors.newFixedThreadPool(2);
pool.submit(task1);
pool.submit(task2);
pool.submit(task3);
pool.shutdown();
}
}
class numTest{
int num = 10;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public numTest(int num) {
super();
this.num = num;
}
public numTest() {
super();
}
}