您的位置:首页 > 编程语言 > Java开发

Java总结篇系列:Java多线程(二)

2016-10-06 18:14 721 查看
四.Java多线程的阻塞状态与线程控制
上文已经提到Java阻塞的几种具体类型。下面分别看下引起Java线程阻塞的主要方法。
1.join()
join —— 让一个线程等待另一个线程完成才继续执行。如A线程线程执行体中调用B线程的join()方法,则A线程被阻塞,知道B线程执行完为止,A才能得以继续执行。

1 public class ThreadTest {
2
3     public static void main(String[] args) {
4
5         MyRunnable myRunnable = new MyRunnable();
6         Thread thread = new Thread(myRunnable);
7
8         for (int i = 0; i < 100; i++) {
9             System.out.println(Thread.currentThread().getName() + " " + i);
10             if (i == 30) {
11                 thread.start();
12                 try {
13                     thread.join();    // main线程需要等待thread线程执行完后才能继续执行
14                 } catch (InterruptedException e) {
15                     e.printStackTrace();
16                 }
17             }
18         }
19     }
20 }
21
22 class MyRunnable implements Runnable {
23
24     @Override
25     public void run() {
26         for (int i = 0; i < 100; i++) {
27             System.out.println(Thread.currentThread().getName() + " " + i);
28         }
29     }
30 }


 
2.sleep()
sleep —— 让当前的正在执行的线程暂停指定的时间,并进入阻塞状态。在其睡眠的时间段内,该线程由于不是处于就绪状态,因此不会得到执行的机会。即使此时系统中没有任何其他可执行的线程,出于sleep()中的线程也不会执行。因此sleep()方法常用来暂停线程执行。
前面有讲到,当调用了新建的线程的start()方法后,线程进入到就绪状态,可能会在接下来的某个时间获取CPU时间片得以执行,如果希望这个新线程必然性的立即执行,直接调用原来线程的sleep(1)即可。

1 public class ThreadTest {
2
3     public static void main(String[] args) {
4
5         MyRunnable myRunnable = new MyRunnable();
6         Thread thread = new Thread(myRunnable);
7
8         for (int i = 0; i < 100; i++) {
9             System.out.println(Thread.currentThread().getName() + " " + i);
10             if (i == 30) {
11                 thread.start();
12                 try {
13                     Thread.sleep(1);   // 使得thread必然能够马上得以执行
14                 } catch (InterruptedException e) {
15                     e.printStackTrace();
16                 }
17             }
18         }
19     }
20 }
21
22 class MyRunnable implements Runnable {
23
24     @Override
25     public void run() {
26         for (int i = 0; i < 100; i++) {
27             System.out.println(Thread.currentThread().getName() + " " + i);
28         }
29     }
30 }


注:睡一个毫秒级够了,因为CPU不会空闲,会切换到新建的线程。
 
3.后台线程(Daemon Thread)
概念/目的:后台线程主要是为其他线程(相对可以称之为前台线程)提供服务,或“守护线程”。如JVM中的垃圾回收线程。
生命周期:后台线程的生命周期与前台线程生命周期有一定关联。主要体现在:当所有的前台线程都进入死亡状态时,后台线程会自动死亡(其实这个也很好理解,因为后台线程存在的目的在于为前台线程服务的,既然所有的前台线程都死亡了,那它自己还留着有什么用...伟大啊 ! !)。
设置后台线程:调用Thread对象的setDaemon(true)方法可以将指定的线程设置为后台线程。

1 public class ThreadTest {
2
3     public static void main(String[] args) {
4         Thread myThread = new MyThread();
5         for (int i = 0; i < 100; i++) {
6             System.out.println("main thread i = " + i);
7             if (i == 20) {
8                 myThread.setDaemon(true);
9                 myThread.start();
10             }
11         }
12     }
13
14 }
15
16 class MyThread extends Thread {
17
18     public void run() {
19         for (int i = 0; i < 100; i++) {
20             System.out.println("i = " + i);
21             try {
22                 Thread.sleep(1);
23             } catch (InterruptedException e) {
24                 // TODO Auto-generated catch block
25                 e.printStackTrace();
26             }
27         }
28     }
29 }


判断线程是否是后台线程:调用thread对象的isDeamon()方法。
注:main线程默认是前台线程,前台线程创建中创建的子线程默认是前台线程,后台线程中创建的线程默认是后台线程。调用setDeamon(true)方法将前台线程设置为后台线程时,需要在start()方法调用之前。前天线程都死亡后,JVM通知后台线程死亡,但从接收指令到作出响应,需要一定的时间。
 
4.改变线程的优先级/setPriority():
每个线程在执行时都具有一定的优先级,优先级高的线程具有较多的执行机会。每个线程默认的优先级都与创建它的线程的优先级相同。main线程默认具有普通优先级。
设置线程优先级:setPriority(int priorityLevel)。参数priorityLevel范围在1-10之间,常用的有如下三个静态常量值:
MAX_PRIORITY:10
MIN_PRIORITY:1
NORM_PRIORITY:5
获取线程优先级:getPriority()。
注:具有较高线程优先级的线程对象仅表示此线程具有较多的执行机会,而非优先执行。

1 public class ThreadTest {
2
3     public static void main(String[] args) {
4         Thread myThread = new MyThread();
5         for (int i = 0; i < 100; i++) {
6             System.out.println("main thread i = " + i);
7             if (i == 20) {
8                 myThread.setPriority(Thread.MAX_PRIORITY);
9                 myThread.start();
10             }
11         }
12     }
13
14 }
15
16 class MyThread extends Thread {
17
18     public void run() {
19         for (int i = 0; i < 100; i++) {
20             System.out.println("i = " + i);
21         }
22     }
23 }


 
5.线程让步:yield()
上一篇博文中已经讲到了yield()的基本作用,同时,yield()方法还与线程优先级有关,当某个线程调用yiled()方法从运行状态转换到就绪状态后,CPU从就绪状态线程队列中只会选择与该线程优先级相同或优先级更高的线程去执行。

1 public class ThreadTest {
2
3     public static void main(String[] args) {
4         Thread myThread1 = new MyThread1();
5         Thread myThread2 = new MyThread2();
6         myThread1.setPriority(Thread.MAX_PRIORITY);
7         myThread2.setPriority(Thread.MIN_PRIORITY);
8         for (int i = 0; i < 100; i++) {
9             System.out.println("main thread i = " + i);
10             if (i == 20) {
11                 myThread1.start();
12                 myThread2.start();
13                 Thread.yield();
14             }
15         }
16     }
17
18 }
19
20 class MyThread1 extends Thread {
21
22     public void run() {
23         for (int i = 0; i < 100; i++) {
24             System.out.println("myThread 1 --  i = " + i);
25         }
26     }
27 }
28
29 class MyThread2 extends Thread {
30
31     public void run() {
32         for (int i = 0; i < 100; i++) {
33             System.out.println("myThread 2 --  i = " + i);
34         }
35     }
36 }


 

--------------------------------------------------------------------------------- 

转载自:http://www.cnblogs.com/lwbqqyumidi/p/3817517.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: