Java多线程与线程同步

  继承Runnable接口实现
public class TicketSaling implements Runnable{
 private int ticket=100;
 private Object obj= new Object();
 
 public void run(){
  while(ticket>0){
   
   synchronized(obj){
    System.out.println(Thread.currentThread().getName()+
      "售出一张票,剩余"+--ticket+"张票");
   }
   try{
    Thread.sleep(10);
   }
   catch(InterruptedException e){
    e.printStackTrace();
   }
   
  }
 }
}
售票窗口的测试类
public class TicketSalingTest {
 public static void  main(String[] args){
  TicketSaling t=new TicketSaling();
  
  Thread t1=new Thread(t,"售票窗口1");
  Thread t2=new Thread(t,"售票窗口2");
  Thread t3=new Thread(t,"售票窗口3");
  Thread t4=new Thread(t,"售票窗口4");
  
  t1.start();
  t2.start();
  t3.start();
  t4.start();
  }
}
存钱 类  采用继承Thread的子类创建线程,采用同步方法实现线程同步,注意互斥资源使用静态资源,同步方法也要定义为静态方法,使得所有对象都是用同一个资源和同一个方法(没有采用静态方法前,会才生线程互斥操作)
public class SaveMoney extends Thread{
 private static int count=0;
 public SaveMoney(String str){
  super(str);
 }
 public synchronized static void saveMoney(){
  System.out.println(Thread.currentThread().getName()+
    "存入100元,银行总资金为"+(count+=100));
 }
 public void run(){
  for(int i=0;i<4;i++){
   saveMoney();
   try{
    Thread.sleep(100);
   }
   catch(Exception e){
    e.printStackTrace();
   }
  }
 }
}
测试类
public class SaveMoneyTest {
 public static void main(String[] args){
  SaveMoney t1= new SaveMoney("老张");
  SaveMoney t2= new SaveMoney("老王");
  
  t1.start();
  t2.start();
 }
}
测试结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200611235937764.png)
最后一问不仅要实现线程同步,还有考虑线程间的通信,这样才能让读写操作能够交替进行。主要使用wait方法和notify方法,也就线程阻塞和线程唤醒。
互斥资源类
public class Data{
 private TreeMap<String,String> data= new TreeMap<String,String>();
 
 boolean isWrited=false;//是否为写入状态
 
 public synchronized void readData(){
  if(!isWrited){
   try{
    wait();
   }catch(InterruptedException e){
    e.printStackTrace();
   }
  }
  System.out.println("读取数据: "+data);
  isWrited=false;
  notify();
 }
 public synchronized void writeData(String name,String sex){
  if(isWrited){
   try{
    wait();
   }
   catch(InterruptedException e){
    e.printStackTrace();
   }
  }
  data.put(name, sex);
  System.out.println("写入数据: "+name+" "+sex);
  isWrited=true;
  notify();
 }
}
写线程
public class WriteThread extends Thread{
 Data data=new Data();
 public WriteThread(Data data){
  this.data=data;
 }
 public void run(){
  data.writeData("小明", "男");
  data.writeData("小红", "女");
  data.writeData("小冉", "女");
  data.writeData("小烨", "男");
 }
}
读线程
public class ReadThread extends Thread{
 Data data=new Data();
 public ReadThread(Data data){
  this.data=data;
 }
 public void run(){
  while(true){
   data.readData();
  }
 }
}
测试类
public class TestThread {
 public static void main(String[] args){
  Data data=new Data();
  
  WriteThread w=new WriteThread(data);
  ReadThread  r=new ReadThread(data);
  
  w.start();
  r.start();
 }
}
注意线程唤醒和阻塞的条件,不然会产生线程死锁。
测试结果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值