线程2(11.26)

本文深入探讨了Java中线程的控制与同步机制,包括使用wait(), notify()和sleep()方法来控制线程的运行状态,展示了如何通过这些方法实现线程间的协作和资源的共享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**thead.sleep() // 让其回到队列中,继续等待、
让线程休眠: super.wait(); 给一个条件,即可进行判断,让线程停止。
super.notify(); 睡眠后 需要手动唤醒,用这个关键字。 **

应用在如下代码中

public class Xianc2 {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Object o =new Object();
  XC1 xc1 = new XC1();
  XC1 xc2 = new XC1();
  xc1.start();
  xc2.start();  
 }
}
class XC1 extends Thread{
 static Object o;
 static int a=0;
 public void XC1(Object o) {
  this.o=o;
 }
 public void run() {
  // TODO Auto-generated method stub
  add();
 }
 public synchronized  void add() { //为其上一个带锁的add方法。
  // TODO Auto-generated method stub
  for (int i = 0; a< 1000; i++) {
   System.out.println(this.getName()+"\t"+a++);
   if(a>500){     
    try {
     super.wait();      //让线程休眠   当a>500时,线程停止
    } catch (Exception e) {
     // TODO: handle exception
     e.printStackTrace();
    }   
   }
  }
 }
 }
public class Xianc2 {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  XC1 xc1 = new XC1();
  XC2 xc2 = new XC2(xc1);
  XC3 xc3 = new XC3(xc1);
  xc2.start();
  xc3.start();  
 }
}
//  sleep  到时间后,会自动进入队列
//   wait  不会自动回到数列,需要控制相同对象资源 激活它
class XC1{
 static int a=0;
 static boolean boo=false;
 public synchronized void add() {
  // TODO Auto-generated method stub
  System.out.println(a++);
  if(a%5==0){
   try {
    System.out.println("add()wait");
    boo=true;
    super.wait();
    System.out.println("aaa()被notify");
   } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
   }
   }
 }
 public synchronized  void add2() { //为其上一个带锁的add方法。
   // TODO Auto-generated method stub
   if(boo){
    try {
     System.out.println("add2唤醒");
     super.notify();
     boo=false;
    } catch (Exception e) {
     // TODO: handle exception
    } 
   }
   else
    try {
     Thread.sleep(200);
    } catch (Exception e) {
     // TODO: handle exception
    }
   }
  }
class XC2 extends Thread{
 XC1  xc1;
 public  XC2(XC1 xc1) {
  this.xc1=xc1;
 }
 public void run() {
  // TODO Auto-generated method stub
  for (int i = 0; i < 1000; i++) {
   xc1.add();
   } 
  }
 }
 class XC3  extends Thread{
 XC1  xc1;
 public  XC3(XC1 xc1) {
  this.xc1=xc1;
 }
 public void run() {
  // TODO Auto-generated method stub
  for (int i = 0; i < 1000; i++) {
   xc1.add2();
  } 
 }
}
 

第三种,定义一个类变量的,没用父类(应该是这样)
在这里插入图片描述

在这里插入图片描述

public class Xianc1 {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  XC_1 xc1 = new XC_1();
  XC_2 xc2 = new XC_2(xc1);
  XC_3 xc3 = new XC_3(xc1);
  xc2.start();
  xc3.start();
 }
}
class XC_1{
  static Object o=new Object();
  static int a=0;
  static boolean boo=false;
  public void wait_method() {
   // TODO Auto-generated method stub
   System.out.println(a++);
   synchronized (o) {
    if(a%5==0){
     try {
      System.out.println("wait_method()wait");
      boo=true;
      Thread.sleep(200);
      o.wait();
      System.out.println("wait_method()被notify");
     } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
     }
    }
   }
  }  
  public boolean notify_method() {
   // TODO Auto-generated method stub  
   synchronized (o) {
    if(boo){
    if(a%5==0){
     try {
      System.out.println("notiify_method()wait");
      o.notify();
      boo=false;
     } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
     }
    }
   }
  }return a<1000;
 }
}
  class XC_2 extends Thread{
   XC_1  xc1;
   public  XC_2(XC_1 xc1) {
    this.xc1=xc1;
   }
   public void run() {
    // TODO Auto-generated method stub
    for (int i = 0; i < 1000; i++) {
     xc1.wait_method();
     } 
    }
   }
  class XC_3  extends Thread{
   XC_1  xc1;
   public  XC_3(XC_1 xc1) {
    this.xc1=xc1;
   }
   public void run() {
    // TODO Auto-generated method stub
   while(xc1.notify_method()){}
   }
  }

守护线程 垃圾回收,守护性能,例如写个日志啥的 对象名.setDaemon(true)
只要main没停止,守护线程就可以执行下去,main要是执行结束,守护线程就结束,

在这里插入图片描述

public class SHxc {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Shou s=new Shou();
  s.start();
  for (int i = 0; i < 1000; i++) {
   System.out.println("main"+i);
   try {
    Thread.sleep(5);
   } catch (Exception e) {
    // TODO: handle exception
   }
  }
 }
}
class Shou extends Thread{
 @Override
 public void run() {
  // TODO Auto-generated method stub
 int a=1;
 while(true){
  System.out.println("\t\t\t"+a++);
  try {
   Thread.sleep(10);
  } catch (Exception e) {
   // TODO: handle exception
  }
 }
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值