• 2009-02-03

    java线程学习 - [Java]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://allantaylor.blogbus.com/logs/34563532.html

    isAlive(),join()的使用
    isAlive()方法在Thread中定义:final bollean isAlive() ,
    所以只能在Thread类的实例或其子类中调用.
    一个更经常使用的方法是调用join()方法来等待另一个线程的结束.它的定义如下:
    final void join() throws InterruptedException
    这个方法一直等待,直到它调用的线程终止.

    Java代码 复制代码
    1. package mythread;   
    2.   
    3. class NewThread implements Runnable {   
    4.     String name; // name of thread   
    5.   
    6.     Thread t;   
    7.   
    8.     NewThread(String threadname) {   
    9.         name = threadname;   
    10.         t = new Thread(this, name);   
    11.         System.out.println("New thread: " + t);   
    12.         t.start(); // Start the thread   
    13.     }   
    14.   
    15.     // This is the entry point for thread.   
    16.     public void run() {   
    17.         try {   
    18.             for (int i = 5; i > 0; i--) {   
    19.                 System.out.println(name + ": " + i);   
    20.                 Thread.sleep(1000);   
    21.             }   
    22.         } catch (InterruptedException e) {   
    23.             System.out.println(name + " interrupted.");   
    24.         }   
    25.         System.out.println(name + " exiting.");   
    26.     }   
    27. }   
    28.   
    29.   
    30.   
    31.   
    32. public class DemoJoinIsAlive {   
    33.     public static void main(String args[]) {   
    34.         NewThread ob1 = new NewThread("One");   
    35.         NewThread ob2 = new NewThread("Two");   
    36.         NewThread ob3 = new NewThread("Three");   
    37.   
    38.         System.out.println("Thread One is alive: " + ob1.t.isAlive());   
    39.         System.out.println("Thread Two is alive: " + ob2.t.isAlive());   
    40.         System.out.println("Thread Three is alive: " + ob3.t.isAlive());   
    41.         // wait for threads to finish   
    42.         try {   
    43.             System.out.println("Waiting for threads to finish.");   
    44.             ob1.t.join();   
    45.             ob2.t.join();   
    46.             ob3.t.join();   
    47.         } catch (InterruptedException e) {   
    48.             System.out.println("Main thread Interrupted");   
    49.         }   
    50.   
    51.         System.out.println("Thread One is alive: " + ob1.t.isAlive());   
    52.         System.out.println("Thread Two is alive: " + ob2.t.isAlive());   
    53.         System.out.println("Thread Three is alive: " + ob3.t.isAlive());   
    54.   
    55.         System.out.println("Main thread exiting.");   
    56.     }   
    57. }  



    同步
    可以用两种方式实现:使用同步方法;同步语句(同步代码块).它们都使用关键字synchronized.
    一.使用同步方法:
    直接上代码:

    Java代码 复制代码
    1. /**  
    2.  * 演示同步方法的使用!!!  
    3.  * NOTES:  
    4.  * java 中每一个对象都有一个隐含锁(或称监控器),  
    5.  * 记住,一旦一个线程进入一个实例的任何同步方法,别的线程将不能进入同一实例的其他同步方法,  
    6.  * 因为它没有持有此实例的(隐含)锁,但是该实例的非同步方法仍然能够被调用.  
    7.  *   
    8.  */  
    9.   
    10. class Callme {   
    11. //比较下面两行方法的声明,当加synchronized时同步,否则不会同步.    
    12.     //  synchronized void call(String msg) {   
    13.         void call(String msg) {   
    14.         System.out.print("[" + msg);   
    15.         try {   
    16.             Thread.sleep(1000);   
    17.         } catch (InterruptedException e) {   
    18.             System.out.println("Interrupted");   
    19.         }   
    20.         System.out.println("]");   
    21.     }   
    22. }   
    23.   
    24. class Caller implements Runnable {   
    25.     String msg;   
    26.   
    27.     Callme target;   
    28.   
    29.     Thread t;   
    30.   
    31.     public Caller(Callme targ, String s) {   
    32.         target = targ;   
    33.         msg = s;   
    34.         t = new Thread(this);   
    35.         t.start();   
    36.     }   
    37.   
    38.     public void run() {   
    39.         target.call(msg);   
    40.     }   
    41. }   
    42.   
    43. class Synch {   
    44.     public static void main(String args[]) {   
    45.         Callme target = new Callme();   
    46.         Caller ob1 = new Caller(target, "Hello");   
    47.         Caller ob2 = new Caller(target, "Synchronized");   
    48.         Caller ob3 = new Caller(target, "World");   
    49.   
    50.         // wait for threads to end   
    51.         try {   
    52.             ob1.t.join();   
    53.             ob2.t.join();   
    54.             ob3.t.join();   
    55.         } catch (InterruptedException e) {   
    56.             System.out.println("Interrupted");   
    57.         }   
    58.     }   
    59. }  




    二.同步语句:

    Java代码 复制代码
    1. /** NOTES:  
    2.  * 在类中创建synchronized方法是一种取得同步的简单有效的方法,但它并不是在所有情况下都有效,为什么?  
    3.  * 考虑下面的情况:假设你想同步访问没有设计为多线程访问的类的对象,也就是说,类不使用synchronized方法,  
    4.  * 或者更进一步,该类不是由你创建,而是由第三方创建的,你并不能访问它的源代码,因此此时不能在类相应的方法中  
    5.  * 增加synchronized关键词.那么怎样同步访问该类的对象呢?幸运的是,这个问题的解决方法非常简单:只需要将对  
    6.  * 该类方法的访问置于一个同步块中.  
    7.  */  
    8. //This program uses a synchronized block.   
    9.   
    10. class Callme2 {   
    11.     void call(String msg) {   
    12.         System.out.print("[" + msg);   
    13.         try {   
    14.             Thread.sleep(1000);   
    15.         } catch (InterruptedException e) {   
    16.             System.out.println("Interrupted");   
    17.         }   
    18.         System.out.println("]");   
    19.     }   
    20. }   
    21.   
    22.   
    23.   
    24.   
    25. class Caller2 implements Runnable {   
    26.     String msg;   
    27.   
    28.     Callme2 target;   
    29.   
    30.     Thread t;   
    31.   
    32.     public Caller2(Callme2 targ, String s) {   
    33.         target = targ;   
    34.         msg = s;   
    35.         t = new Thread(this);   
    36.         t.start();   
    37.     }   
    38.   
    39.     // synchronize calls to call()   
    40.     public void run() {   
    41.         synchronized (target) { // 同步块 synchronized block   
    42.             target.call(msg);   
    43.         }   
    44.     }   
    45. }   
    46.   
    47.   
    48.   
    49.   
    50. public class Synch2 {   
    51.     public static void main(String args[]) {   
    52.         Callme2 target = new Callme2();   
    53.         Caller2 ob1 = new Caller2(target, "Hello");   
    54.         Caller2 ob2 = new Caller2(target, "Synchronized");   
    55.         Caller2 ob3 = new Caller2(target, "World");   
    56.   
    57.         // wait for threads to end   
    58.         try {   
    59.             ob1.t.join();   
    60.             ob2.t.join();   
    61.             ob3.t.join();   
    62.         } catch (InterruptedException e) {   
    63.             System.out.println("Interrupted");   
    64.         }   
    65.     }   
    66. }  

    收藏到:Del.icio.us