join线程

在某个线程中调用其他线程的join()方法,就会使当前线程进入阻塞状态,直到被join线程执行完为止。join方法类似于wait, 通常会在主线程中调用别的线程的join方法,这样可以保证在所有的子线程执行结束之后在主线程中完成一些统一的步骤。
下面是一个例子,

  1. package threads;
  2.  
  3. public class JoinThread extends Thread {
  4. public JoinThread(String name) {
  5. super(name);
  6. }
  7.  
  8. public void run() {
  9. for(int i=0; i<20; i++) {
  10. System.out.println(getName()+" "+i);
  11. }
  12. }
  13.  
  14. public static void main(String[] args) throws InterruptedException {
  15. new JoinThread("new Thread-1").start();
  16. for (int i=0; i<50; i++) {
  17. if(i==20){
  18. JoinThread jt = new JoinThread("new thread-2");
  19. jt.start();
  20. //main线程调用了jt线程的join方法,main线程
  21. //必须等jt执行完之后才能继续执行
  22. jt.join();
  23. }
  24. System.out.println(Thread.currentThread().getName()+" "+i);
  25. }
  26. }
  27.  
  28. }

执行结果,可见当主线程中i=20时,主线程被阻塞了,测试thread-1和thread-2正在执行,一直到thread-2执行结束之后,主线程才继续执行

  1. main 0
  2. new Thread-1 0
  3. main 1
  4. new Thread-1 1
  5. main 2
  6. new Thread-1 2
  7. main 3
  8. new Thread-1 3
  9. main 4
  10. new Thread-1 4
  11. main 5
  12. new Thread-1 5
  13. main 6
  14. new Thread-1 6
  15. main 7
  16. new Thread-1 7
  17. main 8
  18. new Thread-1 8
  19. main 9
  20. new Thread-1 9
  21. main 10
  22. new Thread-1 10
  23. main 11
  24. new Thread-1 11
  25. main 12
  26. new Thread-1 12
  27. main 13
  28. new Thread-1 13
  29. main 14
  30. new Thread-1 14
  31. main 15
  32. new Thread-1 15
  33. main 16
  34. new Thread-1 16
  35. main 17
  36. new Thread-1 17
  37. main 18
  38. new Thread-1 18
  39. main 19
  40. new Thread-1 19
  41. new thread-2 0
  42. new thread-2 1
  43. new thread-2 2
  44. new thread-2 3
  45. new thread-2 4
  46. new thread-2 5
  47. new thread-2 6
  48. new thread-2 7
  49. new thread-2 8
  50. new thread-2 9
  51. new thread-2 10
  52. new thread-2 11
  53. new thread-2 12
  54. new thread-2 13
  55. new thread-2 14
  56. new thread-2 15
  57. new thread-2 16
  58. new thread-2 17
  59. new thread-2 18
  60. new thread-2 19
  61. main 20
  62. main 21
  63. main 22
  64. main 23
  65. main 24
  66. main 25
  67. main 26
  68. main 27
  69. main 28
  70. main 29
  71. main 30
  72. main 31
  73. main 32
  74. main 33
  75. main 34
  76. main 35
  77. main 36
  78. main 37
  79. main 38
  80. main 39
  81. main 40
  82. main 41
  83. main 42
  84. main 43
  85. main 44
  86. main 45
  87. main 46
  88. main 47
  89. main 48
  90. main 49

后台线程:setDaemon

后台线程有个特征就是,如果所有前台线程都死亡,后台线程会自动死亡。JVM的垃圾回收器就是一种典型的后台线程。Thread提供了一个isDaemon方法判断是否为后台线程。

下面是一个例子,注意必须在线程start之前设置(setDaemon(true))为后台线程。

  1. package threads;
  2.  
  3. public class DaemonThread extends Thread {
  4. public void run() {
  5. for(int i=0; i<1000; i++) {
  6. System.out.println(getName()+" "+i);
  7. }
  8. }
  9.  
  10. public static void main(String[] args) {
  11. DaemonThread t = new DaemonThread();
  12. //必须在start之前设置成后台线程
  13. t.setDaemon(true);
  14. t.start();
  15. for (int i=0; i<3; i++) {
  16. System.out.println(Thread.currentThread().getName()+" "+i);
  17. }
  18. //main进程运行到此处时结束,后台进程也会随之结束
  19. }
  20. }

执行结果,可以看到子线程的i并没有累加到1000就结束了,因为子线程被设置成了后台线程,主线程先于子线程结束,子线程也就随之结束了

  1. main 0
  2. Thread-5 0
  3. main 1
  4. Thread-5 1
  5. main 2
  6. Thread-5 2
  7. Thread-5 3
  8. Thread-5 4
  9. ...
  10. Thread-5 199
  11. Thread-5 200
  12. Thread-5 201
  13. Thread-5 202

线程睡眠:sleep

sleep数Thread类的一个静态方法,让当前线程进入阻塞状态。

在sleep指定的时间到达之前,线程都不会获得执行机会,即使有可用的CPU执行片,因此sleep方法常常用来暂停线程。

sleep方法需要抛出一个异常。

下面是一个例子,

  1. package threads;
  2.  
  3. import java.util.Date;
  4.  
  5. public class SleepThread {
  6. public static void main(String[] args) throws InterruptedException {
  7. for (int i=0; i<10; i++) {
  8. System.out.println("time:" + new Date());
  9. Thread.sleep(1000);
  10. }
  11. }
  12. }

执行结果,

  1. time:Wed Nov 16 12:00:58 CST 2016
  2. time:Wed Nov 16 12:00:59 CST 2016
  3. time:Wed Nov 16 12:01:00 CST 2016
  4. time:Wed Nov 16 12:01:01 CST 2016
  5. time:Wed Nov 16 12:01:02 CST 2016
  6. time:Wed Nov 16 12:01:03 CST 2016
  7. time:Wed Nov 16 12:01:04 CST 2016
  8. time:Wed Nov 16 12:01:05 CST 2016
  9. time:Wed Nov 16 12:01:06 CST 2016
  10. time:Wed Nov 16 12:01:07 CST 2016

线程优先级 及 线程让步:yield

优先级

JAVA 使用setPriority改变线程优先级。JVM提供了三个常量可在不同平台使用,分别是MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY,当然也可以使用1。。10之类的数字,但是不同平台定义的数字不同,所以还是推荐用常量。

yield

yield也是Thread类的一个静态方法,它和sleep有些类似,都可以暂停当前线程,不同的是它不会让线程进入阻塞状态,而是直接进入就绪状态,让当前线程重新进入资源竞争中,此时只有优先级等于或者高于当前线程的其他线程才能获取CPU资源执行,否则被yield的线程将重新获取资源继续执行。

下面是一个例子,

  1. package threads;
  2.  
  3. public class YieldThread extends Thread{
  4. public YieldThread(String name) {
  5. super(name);
  6. }
  7.  
  8. public void run() {
  9. for ( int i = 0; i<50; i++) {
  10. System.out.println(getName()+" "+i);
  11. if (i == 20) {
  12. Thread.yield();
  13. }
  14. }
  15. }
  16.  
  17. public static void main(String[] args) throws InterruptedException {
  18. YieldThread yt1 = new YieldThread("thread-1");
  19. yt1.setPriority(Thread.MAX_PRIORITY);
  20. //yt1.setPriority(7);
  21. yt1.start();
  22. Thread.sleep(1);
  23.  
  24. YieldThread yt2 = new YieldThread("thead-2 ");
  25. yt2.setPriority(Thread.MIN_PRIORITY);
  26. //yt2.setPriority(1);
  27.  
  28. yt2.start();
  29. }
  30. }

执行结果,可以看到,因为thread-1优先级比thread-2高,所以无轮是thread-1调用了yield还是thread-2调用了yield,都是thread-1先抢到资源继续执行

  1. thread-1 0
  2. thread-1 1
  3. thread-1 2
  4. thread-1 3
  5. thread-1 4
  6. thread-1 5
  7. thread-1 6
  8. thread-1 7
  9. thread-1 8
  10. thread-1 9
  11. thread-1 10
  12. thread-1 11
  13. thread-1 12
  14. thread-1 13
  15. thread-1 14
  16. thread-1 15
  17. thread-1 16
  18. thread-1 17
  19. thread-1 18
  20. thread-1 19
  21. thread-1 20
  22. thread-1 21
  23. thread-1 22
  24. thread-1 23
  25. thead-2 0
  26. thread-1 24
  27. thead-2 1
  28. thread-1 25
  29. thead-2 2
  30. thread-1 26
  31. thead-2 3
  32. thread-1 27
  33. thead-2 4
  34. thread-1 28
  35. thead-2 5
  36. thread-1 29
  37. thead-2 6
  38. thread-1 30
  39. thead-2 7
  40. thread-1 31
  41. thead-2 8
  42. thread-1 32
  43. thead-2 9
  44. thread-1 33
  45. thead-2 10
  46. thread-1 34
  47. thead-2 11
  48. thread-1 35
  49. thead-2 12
  50. thread-1 36
  51. thead-2 13
  52. thread-1 37
  53. thead-2 14
  54. thread-1 38
  55. thead-2 15
  56. thread-1 39
  57. thead-2 16
  58. thread-1 40
  59. thead-2 17
  60. thread-1 41
  61. thead-2 18
  62. thread-1 42
  63. thead-2 19
  64. thread-1 43
  65. thead-2 20
  66. thread-1 44
  67. thead-2 21
  68. thead-2 22
  69. thread-1 45
  70. thead-2 23
  71. thread-1 46
  72. thead-2 24
  73. thead-2 25
  74. thread-1 47
  75. thead-2 26
  76. thread-1 48
  77. thead-2 27
  78. thread-1 49
  79. thead-2 28
  80. thead-2 29
  81. thead-2 30
  82. thead-2 31
  83. thead-2 32
  84. thead-2 33
  85. thead-2 34
  86. thead-2 35
  87. thead-2 36
  88. thead-2 37
  89. thead-2 38
  90. thead-2 39
  91. thead-2 40
  92. thead-2 41
  93. thead-2 42
  94. thead-2 43
  95. thead-2 44
  96. thead-2 45
  97. thead-2 46
  98. thead-2 47
  99. thead-2 48
  100. thead-2 49

  

  

JAVA基础知识之多线程——控制线程的更多相关文章

  1. Java基础知识(多线程和线程池)

    新建状态: 一个新产生的线程从新状态开始了它的生命周期.它保持这个状态直到程序 start 这个线程. 运行状态:当一个新状态的线程被 start 以后,线程就变成可运行状态,一个线程在此状态下被认为 ...

  2. JAVA基础知识之多线程——线程组和未处理异常

    线程组 Java中的ThreadGroup类表示线程组,在创建新线程时,可以通过构造函数Thread(group...)来指定线程组. 线程组具有以下特征 如果没有显式指定线程组,则新线程属于默认线程 ...

  3. JAVA基础知识之多线程——线程通信

    传统的线程通信 Object提供了三个方法wait(), notify(), notifyAll()在线程之间进行通信,以此来解决线程间执行顺序等问题. wait():释放当前线程的同步监视控制器,并 ...

  4. JAVA基础知识之多线程——线程池

    线程池概念 操作系统或者JVM创建一个线程以及销毁一个线程都需要消耗CPU资源,如果创建或者销毁线程的消耗源远远小于执行一个线程的消耗,则可以忽略不计,但是基本相等或者大于执行线程的消耗,而且需要创建 ...

  5. java基础知识总结--多线程

    1.扩展Java.lang.Thread类 1.1.进程和线程的区别: 进程:每个进程都有自己独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1~n个线程. 线程:同一类线 ...

  6. Java基础加强之多线程篇(线程创建与终止、互斥、通信、本地变量)

    线程创建与终止 线程创建 Thread类与Runnable接口的关系 public interface Runnable { public abstract void run(); } public ...

  7. JAVA基础知识之多线程——三种实现多线程的方法及区别

    所有JAVA线程都必须是Thread或其子类的实例. 继承Thread类创建线程 步骤如下, 定义Thead子类并实现run()方法,run()是线程执行体 创建此子类实例对象,即创建了线程对象 调用 ...

  8. JAVA基础知识系列---进程、线程安全

    1 相关概念 1.1 临界区 保证在某一时刻只有一个线程能访问数据的简便方法,在任意时刻只允许一个线程对资源进行访问.如果有多个线程试图同时访问临界区,那么在有一个线程进入后,其他所有试图访问临界区的 ...

  9. JAVA基础知识之多线程——线程同步

    线程安全问题 多个线程同时访问同一资源的时候有可能会出现信息不一致的情况,这是线程安全问题,下面是一个例子, Account.class , 定义一个Account模型 package threads ...

随机推荐

  1. CSS文档流与块级元素和内联元素

    CSS文档流与块级元素(block).内联元素(inline),之前翻阅不少书籍,看过不少文章, 看到所多的是零碎的CSS布局基本知识,比较表面.看过O'Reilly的<CSS权威指南>, ...

  2. Swift游戏实战-跑酷熊猫 11 欢迎进入物理世界

    物理模拟是一个奇妙的事情,以此著名的游戏有愤怒的小鸟.我们在这节将会一起来了解如何设置重力,设置物理包围体,碰撞的检测. 要点: 设置物理检测的代理: 让主场景遵循SKPhysicsContactDe ...

  3. window.cookie

    本地测试cookie用火狐来测试 首先cookie是document上的一个属性. 先弹出一个cookie alert(document.cookie); //弹出是空的 设置cookie,格式是有一 ...

  4. navicat的简单应用

    首先  创建连接 主机名 : 可以不写名称随意 主机名/IP地址:localhost或者127.0.0.1 都是本机的意思 端口:默认3306   尽量不要改怕与其余端口重复,如有重名端口系统会报错 ...

  5. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...

  6. android设置系统模式

    android 静音与振动1,设置静音和振动静音和振动都属于来电后的动作.所以在设置静音和振动时都只是设置一些标识,并往数据库写入相应标识. 文件:packages/apps/settings/src ...

  7. 实现listview的条目点击后改变背景颜色

    gv_categoryeffect_gridview.setChoiceMode(GridView.CHOICE_MODE_SINGLE);,再设置一个selector的背景选择器 getResour ...

  8. paper 43 :ENDNOTE下载及使用方法简介

    转载来源:http://blog.sciencenet.cn/blog-484734-367968.html 软件下载来源: EndNote v9.0 Final 正式版:http://www.ttd ...

  9. JQuery获取和设置Select选项常用方法总结

    1.获取select 选中的 text: $("#cusChildTypeId").find("option:selected").text(); $(&quo ...

  10. 对linux的根目录执行强制递归移除

    开始开始时使用: #rm -f -r / 提示对根目录使用递归操作很危险,然后就没执行成功,让使用 --no-preserve-root 这个参数. 好吧,反正是虚拟机 于是执行: #rm -f -r ...