鱼C论坛

 找回密码
 立即注册
查看: 2998|回复: 0

[学习笔记] java笔记 多线程 六

[复制链接]
发表于 2017-8-26 01:02:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 可爱的静静 于 2017-8-26 01:05 编辑

多线程:

自动唤醒机制:
在多线程里有一种状态是  wait(); notify();  wait();没有参数的wait() 程序会挂在那里不会自动回复执行 等待线程都放在线程池,notify唤醒线程池里的线程,如果有n多线程,首先唤醒的是第一个被等待的线程。

什么是唤醒机制

举个例子:
看图

例子就是当输入后输入的线程在等待之前唤醒了输出的那条线程
输出后 输出的线程等待之前 唤醒输入线程

  1. public class ds {

  2.         public static void main(String[] args) {
  3.                 Res b=new Res();
  4.                 Output a=new Output(b);
  5.                 Intput c=new Intput(b);
  6.                 Thread d=new Thread(a);
  7.                 Thread e=new Thread(c);
  8.                 e.start();
  9.                 d.start();
  10.                 }
  11. }
  12. class Res{
  13.         String name;
  14.         char sex;
  15.         boolean flag=false;
  16.                
  17. }
  18. class Output implements Runnable{
  19.          private Res r;
  20.          Output(Res r){
  21.                  this.r=r;
  22.                  
  23.          }
  24.          public void run(){
  25.                  int x=0;
  26.                  while(true){
  27.                  if(r.flag)
  28.                         try {r.wait();} catch (Exception e) {};
  29.                    synchronized(r){
  30.                                 if(x==0){
  31.                                    r.name="啦啦啦啦";
  32.                                    r.sex='女';
  33.                                 }
  34.                                 else{
  35.                                         r.name="棒棒棒";
  36.                                         r.sex='男';
  37.                                 }
  38.                                 x=x+1%2;
  39.                                 r.flag=true;
  40.                                 r.notify();
  41.                         }
  42.                 }         
  43.                  
  44.          }
  45.        
  46. }

  47. class Intput implements Runnable{
  48.         private Res r;
  49.          Intput(Res r){
  50.                  this.r=r;
  51.          }
  52.          public void run(){
  53.                  while(true){
  54.                  if(!r.flag)
  55.                         try {r.wait();} catch (Exception e) {};
  56.                         synchronized(r){
  57.                                    System.out.println(r.name+"......"+r.sex);
  58.                                 r.flag=false;
  59.                                 r.notify();
  60.                         }
  61.                 }         
  62.                  
  63.          }
  64.        
  65. }
复制代码

               


Wait;
Notif();
NotifyAll();

都使用在同步中,应为要对等待的监视器(锁)的线程操作
所以要使用在同步中 因为只有同步才具有锁

为什么这些操作线程的方法要定义在object 类中呢?
这些方法再操作票同步中线程是 都必须标识 他们所操作的锁
只有同一个锁上的被等待 和一被同一个锁的notify唤醒 不可以对不同锁中的线程唤醒

而锁可以是任何一个对象 可以被任意对象调用的方法在object 类里

Join方法

当A线程执行到了b线程的join方法时,a线程就被等待 ,等到b线程执行完,a线程才有执行权

  1. public class  save
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Demo a=new Demo();
  6.                 Thread c=new Thread(a);
  7.                 Thread d=new Thread(a);
  8.                 c.start();
  9.        c.join();
  10.                 d.start();

  11.         for (int x=0;x<70;x++)
  12.                         System.out.println(Thread.currentThread()+"........"+x);
  13.                 System.out.println("over");
  14.         }
  15. }
  16. class Demo implements Runnable
  17. {
  18.         public void run(){
  19.                 for (int x=0;x<70;x++)
  20.                         System.out.println(Thread.currentThread()+"........"+x);
  21.         }
  22. }
复制代码



运行结果是:主线程执行到c.join(); 主线程变成等待的状态 只有c.join()相对的线程执行完,主线程才会执行

one.png

评分

参与人数 1鱼币 +4 收起 理由
小甲鱼 + 4

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 07:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表