JAVA作业8(20201107)

1.volatile关键字有什么作用?

解决可见性问题:禁用CPU缓存,直接在内存中操作,提高安全性。

2.编写Java程序模拟烧水泡茶最优工序。
在这里插入图片描述

class XiShuihu implements Runnable{
	public void run(){
        System.out.println("洗水壶!");
        try{
            Thread.sleep(200); //休眠
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class ShaoShui implements Runnable{
	public void run(){
        for(int i=1;i<16;i++){
            System.out.println("烧水的第"+i+"分钟~");
            try{
                Thread.sleep(200); 
            }catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
	
}

class PaoCha implements Runnable{
	public void run() {
		System.out.println("泡茶!");
		try{
			Thread.sleep(200);
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("泡茶结束!");
	}
	
}

class XiChahu implements Runnable{
	public void run(){
        System.out.println("洗茶壶!");
        try{
            Thread.sleep(200); 
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
	
}

class XiChabei implements Runnable{
	public void run(){
        for(int i=1;i<3;i++){
            System.out.println("洗茶杯的第"+i+"分钟~");
            try{
                Thread.sleep(200); 
            }catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
	
}

class NaChaye implements Runnable{
	public void run(){
        System.out.println("拿茶叶!");
        try{
            Thread.sleep(200); 
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
	
}

public class ZhuCha{
	public static void main(String[]args) {
	XiShuihu xiShuihu=new XiShuihu();
	ShaoShui shaoShui=new ShaoShui();
	PaoCha paoCha=new PaoCha();
	XiChahu xiChahu=new XiChahu();
	XiChabei xiChabei=new XiChabei();
	NaChaye naChaye=new NaChaye();
	Thread t1=new Thread (xiShuihu);
	Thread t2=new Thread (shaoShui);
	Thread t3=new Thread (paoCha);
	Thread t4=new Thread (xiChahu);
	Thread t5=new Thread (xiChabei);
	Thread t6=new Thread (naChaye);
	
	t1.start();
	try {
        t1.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
	
	t2.start();
	
	t4.start();
    try {
        t4.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    t5.start();
    try {
        t5.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    t6.start();
    try {
        t6.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    
	try {
        t2.join();
        
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
	t3.start();
}
}

其中一种输出:
在这里插入图片描述

  1. 编写一个基于多线程的生产者/消费者Java应用,各产生10个生产者和消费者线程,共享一个缓冲区队列(长度自设),生产者线程将产品放入到缓冲区,消费者线程从缓冲区取出产品。(选做)
class Chanpin                              //物品类
{
	volatile private int num;
    //布尔标志              
    volatile private boolean isChanpin = false;   
    
    //put设为同步方法
    synchronized void put(int i) {
      while(isChanpin){
        try{ wait();} //线程等待
        catch(Exception e){} 
      }
      num = num + i; 
      System.out.println("生产"+i+"个产品 总产品:"+num);
      isChanpin = true;//设置标志
      notifyAll(); //唤醒等待资源的所有线程
   }
    synchronized int get(int i) {//同步方法 
        while(!isChanpin){
            try { wait();}
            catch(Exception e){} 
         }
         if (num>i)
        	 num = num - i;         
         else {  
              i = num;
              num = 0;                    
         }
         System.out.println("取走"+i+"个产品 总产品:"+num);
         isChanpin = false;
         notifyAll(); 
         return i;                      
        } 
}

class  Shengchanzhe implements Runnable            
{
    private Chanpin a1;     
    public Shengchanzhe(Chanpin a1)
    {
        this.a1 = a1;
    }
    public void run()
    {
        while(true){
			a1.put(100);			
	    }
    }
}
class Xiaofeizhe implements Runnable            
{
    private Chanpin a1;
    public Xiaofeizhe(Chanpin a1)
    {this.a1 = a1 ;}
    public void run()
    {       
		while(true){		
			a1.get(100);			
		}		
    }
}
public class Commu{
	public static void main(String[] args){
		Chanpin a1 = new Chanpin();
       new Thread(new Shengchanzhe(a1)).start();//1
       new Thread(new Shengchanzhe(a1)).start();//2
       new Thread(new Shengchanzhe(a1)).start();//3
       new Thread(new Shengchanzhe(a1)).start();//4
       new Thread(new Shengchanzhe(a1)).start();//5
       new Thread(new Shengchanzhe(a1)).start();//6
       new Thread(new Shengchanzhe(a1)).start();//7
       new Thread(new Shengchanzhe(a1)).start();//8
       new Thread(new Shengchanzhe(a1)).start();//9
       new Thread(new Shengchanzhe(a1)).start();//10
       new Thread(new Xiaofeizhe(a1)).start();//1
       new Thread(new Xiaofeizhe(a1)).start();//2
       new Thread(new Xiaofeizhe(a1)).start();//3
       new Thread(new Xiaofeizhe(a1)).start();//4
       new Thread(new Xiaofeizhe(a1)).start();//5
       new Thread(new Xiaofeizhe(a1)).start();//6
       new Thread(new Xiaofeizhe(a1)).start();//7
       new Thread(new Xiaofeizhe(a1)).start();//8
       new Thread(new Xiaofeizhe(a1)).start();//9
       new Thread(new Xiaofeizhe(a1)).start();//10
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值