Java线程:线程安全类和Callable与Future(有返回值的线程)

来源:互联网 发布:粒子群算法的具体应用 编辑:程序博客网 时间:2024/05/22 08:06
一、线程安全类

  当一个类已经很好的同步以保护它的数据时,这个类就称为线程安全的。当一个集合是安全的,有两个线程在操作同一个集合对象,当第一个线程查询集合非空后,删除集合中所有元素的时候,第二个线程也来执行与第一个线程相同的操作,也许第一个线程查询后,第二个也查出非空,但是此时明显是不对的。如:

 1 public class NameList {  2     private List nameList = Collections.synchronizedList(new LinkedList());  3  4     public void add(String name) {  5         nameList.add(name);  6     }  7  8     public String removeFirst() {  9         if (nameList.size() > 0) { 10             return (String) nameList.remove(0); 11         } else { 12             return null; 13         } 14     } 15 }
 1 public class Test {  2     public static void main(String[] args) {  3         final NameList nl = new NameList();  4         nl.add("aaa");  5         class NameDropper extends Thread{  6             public void run(){  7                 String name = nl.removeFirst();  8                 System.out.println(name);  9             } 10         } 11 12         Thread t1 = new NameDropper(); 13         Thread t2 = new NameDropper(); 14         t1.start(); 15         t2.start(); 16     } 17 }

  虽然集合对象private List nameList=Collections.synchronizedList(new LinkedList())是同步的,但是程序并不是线程安全的。

  原因是:一个线程操作列表的过程无法阻止另一个线程对列表的其他操作。解决办法是:在操作集合对象的NameList上面做一个同步。改写后的代码为:

 1 public class NameList {  2     private List nameList = Collections.synchronizedList(new LinkedList());  3  4     public synchronized void add(String name) {  5         nameList.add(name);  6     }  7  8     public synchronized String removeFirst() {  9         if (nameList.size() > 0) { 10             return (String) nameList.remove(0); 11         } else { 12             return null; 13         } 14     } 15 }

  此时,一个线程访问其中一个方法时,其他线程等待。

二、Callable与Future

  通过实现Callable接口实现有返回值的任务,与Runnable接口处理无返回值的任务类似。

执行了Callable任务后,可以获得一个Future对象,在该对象上调用get就可以获得Callable任务返回的Object了。如:

 1 package Thread; 2  3 import java.util.concurrent.Callable; 4 import java.util.concurrent.ExecutionException; 5 import java.util.concurrent.ExecutorService; 6 import java.util.concurrent.Executors; 7 import java.util.concurrent.Future; 8  9 public class CallableTest {10     public static void main(String[] args)throws ExecutionException,InterruptedException{11         ExecutorService pool=Executors.newFixedThreadPool(2);12         Callable c1=new MyCallable("A");13         Callable c2=new MyCallable("B");14         Future f1=pool.submit(c1);15         Future f2=pool.submit(c2);16         System.out.println(">>>"+f1.get().toString());17         System.out.println(">>>"+f2.get().toString());18         pool.shutdown();19     }20 }21 class MyCallable implements Callable{22     private String oid;23     MyCallable(String oid){24         this.oid=oid;25     }26     public Object call()throws Exception{27         return oid+"任务返回的内容";28     }29     30 }
View Code
1 >>>A任务返回的内容2 >>>B任务返回的内容

 

原创粉丝点击