习题解答chapter07

题目

  1. volatile关键字有什么作用?
  2. 编写Java程序模拟烧水泡茶最优工序。
  3. 编写一个基于多线程的生产者/消费者Java应用, 各产生10个生产者和消费者线程,共享一个缓冲区队列(长度自设),生产者线程将产品放入到缓冲区,消费者线程从缓冲区取出产品。(选做)

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

  • volatile可将多个线程之间共享的变量声明为不稳定的,可以使共享变量只能从主存中读取、更改。之所以使用volatile声明,是因为线程可以从主存中读取变量,然后保存在寄存器中,相当于生成副本,在寄存器中进行快速处理操作,该线程结束后再于主存中的变量同步。但是当多个线程共享这个变量时,可能会出现,前面线程并未结束,却已改变变量的值这种情况。此时又来一个线程,它会从主存中读取变量,造成数据不一致。此时,volatile声明可以使之保持一致。
class Resourceplus implements Runnable {
    volatile public int i;
    volatile public Integer it;

    public Resourceplus(int _i) {
        i = _i;
        it = new Integer(i);
    }

    public void run() {
        while (true) {
            synchronized (it) {
                if (i > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (Exception e) {
                    }
                    i--;
                    System.out.println(Thread.currentThread().getName() + "  " + i);
                } else {
                    System.out.println(Thread.currentThread().getName());
                    break;
                }
            }
        }
    }
}

public class TestSecurityPlus {
    public static void main(String[] args) {
        Resourceplus m = new Resourceplus(10);
        Thread t1 = new Thread(m);
        Thread t2 = new Thread(m);
        t1.start();
        t2.start();
    }
}

2. 编写Java程序模拟烧水泡茶最优工序。

package bookcode.ex9.cnblogs;

class XishuiHu extends Thread{
    public void run(){
        System.out.println("开始洗水壶");
        try {
            Thread.sleep(99);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }
        System.out.println("结束洗水壶");
    }
}

class ShaoShui extends Thread{
    public void run(){
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("开始烧水");
        try {
            Thread.sleep(1600);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }
        System.out.println("结束烧水");
    }
}
class XiChaHu extends Thread{
    public void run(){
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("开始洗茶壶");
        try {
            Thread.sleep(200);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }
        System.out.println("结束洗茶壶");
    }
}
class XiChaBei extends Thread{
    public void run(){
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("开始洗茶杯");
        try {
            Thread.sleep(400);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }
        System.out.println("结束洗茶杯");
    }
}
class NaChaYe extends Thread{
    public void run(){
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("开始拿茶叶");
        try {
            Thread.sleep(500);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }
        System.out.println("结束拿茶叶");
    }
}
class PaoCha extends Thread{
    public void run(){
        try {
            Thread.sleep(1601);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("泡茶,请品尝!");
    }
}
public class PaoChaYouHua {
    public static void main(String args[]){
        long start, end, startb, endb;
        Thread a = new XishuiHu();
        Thread b = new ShaoShui();
        Thread c = new XiChaHu();
        Thread d = new XiChaBei();
        Thread e = new NaChaYe();
        Thread f = new PaoCha();

        a.start();
        try {
//            a.join(100);
            Thread.sleep(99);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }

        b.start();
        try {
//            b.join(200);
            Thread.sleep(200);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }

        c.start();
        try {
            c.join(400);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }

        d.start();
        try {
            d.join(500);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }

        e.start();
        try {
            e.join(600);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }

        f.start();
    }

}

3. 编写一个基于多线程的生产者/消费者Java应用, 各产生10个生产者和消费者线程,共享一个缓冲区队列(长度自设),生产者线程将产品放入到缓冲区,消费者线程从缓冲区取出产品。(选做)

class Account                              
{
    volatile private int value;
    synchronized void put(int i)                       
	{
    	try {
			Thread.sleep(300);
		} catch (InterruptedException e) {
		}
		value = value + i; 
		System.out.println(Thread.currentThread().getName()+" 生产者--存入了 "+i+" 余量为:"+value);
	}
    synchronized int get(int i)                         
    {  
    	try {
			Thread.sleep(300);
		} catch (InterruptedException e) {
		}
		if (value>i)
				value = value - i;             
			else                               
			{   i = value;
				value = 0;                     
			}
		System.out.println(Thread.currentThread().getName()+ " 消费者--消费了 "+i+" 余量为:"+value);
      return i;                      
    }    
}
class  Save implements Runnable            
{
    private Account a1;     
    public Save(Account a1)
    {
        this.a1 = a1;
    }
    public void run()
    {
        while(true){
			a1.put(1);			
	    }
    }
}
class Fetch implements Runnable            
{
    private Account a1;
    public Fetch(Account a1)
    {this.a1 = a1 ;}
    public void run()
    {       
		while(true){		
			a1.get(1);			
		}		
    }
}
public class Test{
	public static void main(String[] args){
       Account a1 = new Account();
       new Thread(new Save(a1)).start(); 
       new Thread(new Save(a1)).start();
       new Thread(new Save(a1)).start();
       new Thread(new Save(a1)).start();
       new Thread(new Save(a1)).start();
       new Thread(new Save(a1)).start(); 
       new Thread(new Save(a1)).start();
       new Thread(new Save(a1)).start();
       new Thread(new Save(a1)).start();
       new Thread(new Save(a1)).start();
       //10个生产者和消费者
       new Thread(new Fetch(a1)).start();
       new Thread(new Fetch(a1)).start();
       new Thread(new Fetch(a1)).start();
       new Thread(new Fetch(a1)).start();
       new Thread(new Fetch(a1)).start();
       new Thread(new Fetch(a1)).start();
       new Thread(new Fetch(a1)).start();
       new Thread(new Fetch(a1)).start();
       new Thread(new Fetch(a1)).start();
       new Thread(new Fetch(a1)).start();
	}
}
posted @ 2020-11-14 10:31  iiYing  阅读(95)  评论(0编辑  收藏  举报