Java 中Thread的sleep、join方法解析
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java 中Thread的sleep、join方法解析,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含10728字,纯文字阅读大概需要16分钟。
内容图文
1.Thread中sleep方法作用是使当前线程等待,其他线程开始执行,如果有线程锁,sleep不会让出锁
没有加锁代码如下:
public class SynchronizedSleepMethod { public static void main(String[] args) { MoneyMethod moneyMethod = new MoneyMethod(); for (int i = 0; i < 10; i++) { Thread t = new Thread(new MyThread4(moneyMethod), "t1" + i); t.start(); } for (int i = 0; i < 10; i++) { Thread t = new Thread(new MyThread5(moneyMethod), "t2" + i); t.start(); } } } class MyThread4 implements Runnable { MoneyMethod moneyMethod; /** * */public MyThread4(MoneyMethod moneyMethod) { // TODO Auto-generated constructor stubthis.moneyMethod = moneyMethod; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override publicvoid run() { try { Thread.sleep(1000); moneyMethod.addMoney(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class MyThread5 implements Runnable { MoneyMethod moneyMethod; /** * */public MyThread5(MoneyMethod moneyMethod) { // TODO Auto-generated constructor stubthis.moneyMethod = moneyMethod; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override publicvoid run() { try { Thread.sleep(1000); moneyMethod.subMoney(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println(moneyMethod.money); // TODO Auto-generated method stub } } class MoneyMethod { int money = 200; publicvoid addMoney() throws InterruptedException { System.out.println(Thread.currentThread().getName() + ":::run:::" + money); money++; } publicvoid subMoney() throws InterruptedException { System.out.println(Thread.currentThread().getName() + ":::run:::" + money); money--; } }
结果如下:
t10:::run:::202
t12:::run:::202
t11:::run:::202
t13:::run:::203
t15:::run:::205
t14:::run:::204
t17:::run:::207
t19:::run:::209
t18:::run:::209
t16:::run:::207
t21:::run:::207
t24:::run:::205
t22:::run:::206
t20:::run:::206
t29:::run:::203
t28:::run:::203
t26:::run:::202
t27:::run:::201
t23:::run:::200
t25:::run:::199
加锁代码如下:
public class SynchronizedSleepMethod { public static void main(String[] args) { MoneyMethod moneyMethod = new MoneyMethod(); for (int i = 0; i < 10; i++) { Thread t = new Thread(new MyThread4(moneyMethod), "t1" + i); t.start(); } for (int i = 0; i < 10; i++) { Thread t = new Thread(new MyThread5(moneyMethod), "t2" + i); t.start(); } } } class MyThread4 implements Runnable { MoneyMethod moneyMethod; /** * */public MyThread4(MoneyMethod moneyMethod) { // TODO Auto-generated constructor stubthis.moneyMethod = moneyMethod; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override publicvoid run() { try { Thread.sleep(1000); moneyMethod.addMoney(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class MyThread5 implements Runnable { MoneyMethod moneyMethod; /** * */public MyThread5(MoneyMethod moneyMethod) { // TODO Auto-generated constructor stubthis.moneyMethod = moneyMethod; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override publicvoid run() { try { Thread.sleep(1000); moneyMethod.subMoney(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println(moneyMethod.money); // TODO Auto-generated method stub } } class MoneyMethod { int money = 200; publicsynchronizedvoid addMoney() throws InterruptedException { money++; System.out.println(Thread.currentThread().getName() + ":::run:::" + money); } publicsynchronizedvoid subMoney() throws InterruptedException { money--; System.out.println(Thread.currentThread().getName() + ":::run:::" + money); } }
结果如下:
t10:::run:::201 t18:::run:::202 t17:::run:::203 t12:::run:::204 t13:::run:::205 t14:::run:::206 t15:::run:::207 t16:::run:::208 t11:::run:::209 t19:::run:::210 t22:::run:::209 t21:::run:::208 t20:::run:::207 t25:::run:::206 t24:::run:::205 t23:::run:::204 t26:::run:::203 t27:::run:::202 t29:::run:::201 t28:::run:::200
2.Thread中join()方法阻塞调用此方法的线程(calling thread),直到线程t完成,此线程再继续;
没有join()方法代码如下:
public class JoinMethod { public static void main(String[] args) throws InterruptedException { MoneyMethod2 moneyMethod = new MoneyMethod2(); for (int i = 0; i < 5; i++) { Thread t = new Thread(new MyThread4(moneyMethod), "t1" + i); t.start(); // t.join(300); System.out.println(Thread.currentThread().getName() + ":::run:::1"+i); } for (int i = 0; i < 5; i++) { Thread t = new Thread(new MyThread5(moneyMethod), "t2" + i); t.start(); // t.join(300); System.out.println(Thread.currentThread().getName() + ":::run:::2"+i); } System.out.println(Thread.currentThread().getName() + ":::finish:::"); } } class MyThread4 implements Runnable { MoneyMethod2 moneyMethod; /** * */public MyThread4(MoneyMethod2 moneyMethod) { // TODO Auto-generated constructor stubthis.moneyMethod = moneyMethod; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override publicvoid run() { try { Thread.sleep(2000); moneyMethod.addMoney(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class MyThread5 implements Runnable { MoneyMethod2 moneyMethod; /** * */public MyThread5(MoneyMethod2 moneyMethod) { // TODO Auto-generated constructor stubthis.moneyMethod = moneyMethod; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override publicvoid run() { try { Thread.sleep(2000); moneyMethod.subMoney(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println(moneyMethod.money); // TODO Auto-generated method stub } } class MoneyMethod2 { int money = 200; publicvoid addMoney() throws InterruptedException { money++; System.out.println(Thread.currentThread().getName() + ":::add:::" + money); } publicvoid subMoney() throws InterruptedException { money--; System.out.println(Thread.currentThread().getName() + ":::sub:::" + money); } }
结果:主线程会提前走掉,然后子线程执行
main:::run:::10 main:::run:::11 main:::run:::12 main:::run:::13 main:::run:::14 main:::run:::20 main:::run:::21 main:::run:::22 main:::run:::23 main:::run:::24 main:::finish::: t10:::add:::201 t12:::add:::202 t11:::add:::203 t13:::add:::204 t14:::add:::205 t21:::sub:::203 t20:::sub:::203 t24:::sub:::201 t22:::sub:::200 t23:::sub:::201
添加了join后,join的等待时间>线程执行时间,代码如下:
public class JoinMethod { public static void main(String[] args) throws InterruptedException { MoneyMethod2 moneyMethod = new MoneyMethod2(); for (int i = 0; i < 5; i++) { Thread t = new Thread(new MyThread4(moneyMethod), "t1" + i); t.start(); t.join(3000); System.out.println(Thread.currentThread().getName() + ":::run:::1"+i); } for (int i = 0; i < 5; i++) { Thread t = new Thread(new MyThread5(moneyMethod), "t2" + i); t.start(); t.join(3000); System.out.println(Thread.currentThread().getName() + ":::run:::2"+i); } System.out.println(Thread.currentThread().getName() + ":::finish:::"); } } class MyThread4 implements Runnable { MoneyMethod2 moneyMethod; /** * */public MyThread4(MoneyMethod2 moneyMethod) { // TODO Auto-generated constructor stubthis.moneyMethod = moneyMethod; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override publicvoid run() { try { Thread.sleep(2000); moneyMethod.addMoney(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class MyThread5 implements Runnable { MoneyMethod2 moneyMethod; /** * */public MyThread5(MoneyMethod2 moneyMethod) { // TODO Auto-generated constructor stubthis.moneyMethod = moneyMethod; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override publicvoid run() { try { Thread.sleep(2000); moneyMethod.subMoney(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println(moneyMethod.money); // TODO Auto-generated method stub } } class MoneyMethod2 { int money = 200; publicvoid addMoney() throws InterruptedException { money++; System.out.println(Thread.currentThread().getName() + ":::add:::" + money); } publicvoid subMoney() throws InterruptedException { money--; System.out.println(Thread.currentThread().getName() + ":::sub:::" + money); } }
结果如下:
t10:::add:::201 main:::run:::10 t11:::add:::202 main:::run:::11 t12:::add:::203 main:::run:::12 t13:::add:::204 main:::run:::13 t14:::add:::205 main:::run:::14 t20:::sub:::204 main:::run:::20 t21:::sub:::203 main:::run:::21 t22:::sub:::202 main:::run:::22 t23:::sub:::201 main:::run:::23 t24:::sub:::200 main:::run:::24 main:::finish:::
加入join方法后,线程执行时间>join的等待时间时,代码如下:
public class JoinMethod { public static void main(String[] args) throws InterruptedException { MoneyMethod2 moneyMethod = new MoneyMethod2(); for (int i = 0; i < 5; i++) { Thread t = new Thread(new MyThread4(moneyMethod), "t1" + i); t.start(); t.join(300); System.out.println(Thread.currentThread().getName() + ":::run:::1"+i); } for (int i = 0; i < 5; i++) { Thread t = new Thread(new MyThread5(moneyMethod), "t2" + i); t.start(); t.join(300); System.out.println(Thread.currentThread().getName() + ":::run:::2"+i); } System.out.println(Thread.currentThread().getName() + ":::finish:::"); } } class MyThread4 implements Runnable { MoneyMethod2 moneyMethod; /** * */public MyThread4(MoneyMethod2 moneyMethod) { // TODO Auto-generated constructor stubthis.moneyMethod = moneyMethod; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override publicvoid run() { try { Thread.sleep(2000); moneyMethod.addMoney(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class MyThread5 implements Runnable { MoneyMethod2 moneyMethod; /** * */public MyThread5(MoneyMethod2 moneyMethod) { // TODO Auto-generated constructor stubthis.moneyMethod = moneyMethod; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override publicvoid run() { try { Thread.sleep(2000); moneyMethod.subMoney(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println(moneyMethod.money); // TODO Auto-generated method stub } } class MoneyMethod2 { int money = 200; publicvoid addMoney() throws InterruptedException { money++; System.out.println(Thread.currentThread().getName() + ":::add:::" + money); } publicvoid subMoney() throws InterruptedException { money--; System.out.println(Thread.currentThread().getName() + ":::sub:::" + money); } }
结果如下:
main:::run:::10 main:::run:::11 main:::run:::12 main:::run:::13 main:::run:::14 main:::run:::20 t10:::add:::201 main:::run:::21 t11:::add:::202 main:::run:::22 t12:::add:::203 main:::run:::23 t13:::add:::204 main:::run:::24 main:::finish::: t14:::add:::205 t20:::sub:::204 t21:::sub:::203 t22:::sub:::202 t23:::sub:::201 t24:::sub:::200
在以上代码中可以发现,在加入join()方法后主线程还是会提前走,但是所有子线程会按照执行顺序执行
原文:https://www.cnblogs.com/vstarcui/p/9841769.html
内容总结
以上是互联网集市为您收集整理的Java 中Thread的sleep、join方法解析全部内容,希望文章能够帮你解决Java 中Thread的sleep、join方法解析所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。