Java多线程(2)

Java多线程(2)
 加入到某个线程
         一个线程可以在其他线程之上调用join()方法,其效果是等待一段时间直到第二个线程结束才继续执行。
         对join()方法的调用可以被中断,做法是在调用线程上调用interrupt()方法,这时需要try-catch子句。以下是一例:
 1 package  multithread;
 2
 3
 4 class  JoinedThread  extends  Thread  {
 5    private int lastTime;
 6    public JoinedThread(String name, int ltime) {
 7        super(name);
 8        this.lastTime = ltime;
 9        start();
10    }

11    
12    public void run() {
13        try {
14            sleep(lastTime);
15        }
 catch(InterruptedException e) {
16            System.out.println(getName() + " was interrupted. " );
17            return;
18        }

19        System.out.println(getName() + " has awakened");
20    }

21}

22
23 class  JoiningThread  extends  Thread  {
24    private JoinedThread joinedThread;
25    public JoiningThread(String name, JoinedThread jthread) {
26        super(name);
27        this.joinedThread = jthread;
28        start();
29    }

30    
31    public void run() {
32        try {
33            joinedThread.join();
34        }
 catch(InterruptedException e) {
35        }

36        System.out.println(getName() + " join finished");
37    }

38}

39 public   class  TestJoin  {
40
41    /** *//**
42     * @param args
43     */

44    public static void main(String[] args) {
45        // TODO Auto-generated method stub
46        JoinedThread jthread1 = new JoinedThread("JoinedThread1"2000);
47        JoinedThread jthread2 = new JoinedThread("JoinedThread2"2000);
48        JoiningThread waitThread1 = new JoiningThread("WaitingThread1", jthread1);
49        JoiningThread waitThread2 = new JoiningThread("WaitingThread2", jthread2);
50        jthread2.interrupt();
51    }

52
53}
    我的运行结果是:
JoinedThread2 was interrupted. 
WaitingThread2 join finished
JoinedThread1 has awakened
WaitingThread1 join finished
    JoiningThread将在JoinedThread上调用join()方法来等待JoinedThread的sleep动作结束,当JoinedThread运行结束或者被中断,JoiningThread将一起结束。

创建线程的另一种方法
       
 如果你的类已经继承了其他的类,可以通过实现Runnable接口作为替代。例如:
 1 package  multithread;
 2
 3 public   class  MyThread  implements  Runnable {
 4
 5    
 6
 7    public void run() {
 8        // TODO Auto-generated method stub
 9        System.out.println("My runnable thread");
10    }

11    
12    public static void main(String args[]) {
13        MyThread t = new MyThread();
14        new Thread(t).start();
15    }

16}

17
    这里值得注意的事实,虽然实现了Runnable接口,实现了run()方法,但MyThread类并不具有线程的特性,它仅仅是具有run()方法。所以需要另外建立一个线程对象,将MyThread作为参数传递给构造函数,这样线程对象就可以调用run()方法了。当线程只是做一些辅助工作,而不是类的基本功能时,使用内部类是更好的选择:
 1 package  multithread;
 2
 3 public   class  InnerThread  {
 4    Thread t;
 5    
 6    public InnerThread() {
 7        t = new Thread() {
 8            public void run() {
 9                System.out.println("Inner thread is running");
10            }

11        }
;
12    }

13    
14    public void run() {
15        t.start();
16    }

17    /** *//**
18     * @param args
19     */

20    public static void main(String[] args) {
21        // TODO Auto-generated method stub
22        InnerThread in = new InnerThread();
23        in.run();
24    }

25
26}


你可能感兴趣的:(Java多线程(2))