千锋Java&Day29课后作业

死锁,线程池

8.(线程同步)有下面代码

class MyThread extends Thread{
      private String data;
     public MyThread(String data){
          this.data = data;
     }
     public void run(){
          for(int i = 0; i<100; i++){
             System.out.println(data);
          }
     }
}
public class TestMyThread{
      public static void main(String args[]){
          Thread t1 = new MyThread(“aaa”);
          Thread t2 = new MyThread(“bbb”);
          t1.start();
          t2.start();
      }
 }
 现希望能够同步的输出 aaa 和 bbb,
 即一次输出 100 个 aaa 或 bbb,
 输出这两个字符串时没有交互。
 为了达到上述目的,要对原代码进行修改。
 以下哪些修改方式能够得到想要的结果?
 A.把第 6 行改为 public synchronized void run()
 B.把 run 方法中所有的内容都放在 synchronized(data)代码块中
 C.把 run 方法中所有的内容都放在 synchronized(System.out)代码块中
AC

9.(线程综合)代码改错

class MyThread1 implements Runnable{ 
      public void run() {
           for(int i = 0; i<100; i++){ 
                this.sleep((int)(Math.random()*1000));  
                    System.out.println(“hello”);
           }
      }
}
class MyThread2 extends Thread{
        public void run() throws Exception {
             for(int i = 0; i<100; i++){ 
               this.sleep((int)(Math.random()*1000)); 
                    System.out.println(“world”);
             }
        }
}
public class TestMyThread{
       public static void main(String args[]){
            Runnable t1 = new MyThread1(); 
            Thread t2 = new MyThread2();
            t1.start();
            t2.start();
       }
}
class MyThread1 implements Runnable{
	public void run() {	
	   	for(int i=0;i<100;i++) {		
	   		try {			
	   			//1.在Runnable接口中,没有sleep方法			
	   				//必须用Thread调用sleep方法
	        	Thread.sleep((int)Math.random()*1000);		
	      	} catch (InterruptedException e) {			
	      			e.printStackTrace();	
	  		}			
	  		   System.out.println("hello");	
	  			}	
	  }
}
class MyThread2 extends Thread{
       	public void run() {	
    	     for(int i=0;i<100;i++) {		
    	     	try {			
    	     		//2.不能声明异常,必须手动捕获异常	
    	    			this.sleep((int)Math.random()*1000);		
    	    	} catch (InterruptedException e) {			
    	      		e.printStackTrace();			
    	       }		
    	       	System.out.println("world");	
    	      	}	
    	  }
  }
  public class TestMyThread {
     	public static void main(String[] args) {		
     	     //3.t1引用类型应为Thread 		
     	     Thread t1 = new Thread(new MyThread1());	
     	     	Thread t2 = new MyThread2();		
     	     	t1.start();		
     	     	t2.start();
     	}
  }

课堂案例

死锁(男孩女孩抢筷子)

public class TestDeadLock {
	public static void main(String[] args) {	
		LeftChopstick left = new LeftChopstick();		
		RightChopstick right = new RightChopstick();		
		Thread boy = new Thread(new Boy(left, right));		
		Thread girl = new Thread(new Girl(left, right));		
		boy.start();		
		girl.start();	
	}
}
class LeftChopstick {	
       String name = "左筷子";
 }
 class RightChopstick {	
    String name = "右筷子";
 }
 class Boy implements Runnable {
      	LeftChopstick left;	
      	RightChopstick right;	
   	public Boy(LeftChopstick left, RightChopstick right) {	
   	  	this.left = left;		
   	  	this.right = right;	
   	 }	
   	 @Override	
   	   public void run() {	
   	       	System.out.println("男孩要左筷子");		
   	       	synchronized (left) {			
   	       	try {				
   	       	    System.out.println("男孩礼让");			
   	       	  	left.wait();			
   	       	 } catch (InterruptedException e) {	
   	       	     	e.printStackTrace();		
   	       	}		
   	       		System.out.println("男孩拿到左筷子,准备拿右筷子");	
   	       			synchronized (right) {
   	       				System.out.println("男孩拿到右筷子,开始吃饭");
   	       	}	
   	  	}
   	}
 }
 class Girl implements Runnable {	
    LeftChopstick left;	
    RightChopstick right;	
    public Girl(LeftChopstick left, RightChopstick right) {	
    	this.left = left;		
    	this.right = right;	
    	}	
    	@Override	
    	public void run() {		
    	   System.out.println("女孩要右筷子");		
    	     synchronized (right) {			
    	        System.out.println("女孩拿到右筷子,准备拿左筷子");
    	        	synchronized (left) {		
    	        		System.out.println("女孩拿到左筷子,开始吃饭");
    	        				left.notify();		
    	        	}		
    	  }
   	}
}

生产者与消费者

public class TestProductorCustomer {
	public static void main(String[] args) {		
	   Shop shop = new Shop();		
	   Thread p = new Thread(new Productor(shop), "生产者");		
	   Thread c = new Thread(new Customer(shop), "消费者");
	   		p.start();		
	   		c.start();	
	  }
	}
class Productor implements Runnable {	
	       Shop shop;
	  	public Productor(Shop shop) {		
	  	     this.shop = shop;	
	  	 }
	  	public void run() {		
	  	   // 生产商品存放到shop里		
	  	   for (int i = 1; i <= 10; i++) {		
	  	     	try {				
	  	     	    this.shop.saveGoods(new Goods(i));		
	  	     	} catch (InterruptedException e) {	
	  	     		e.printStackTrace();			
	  	     }		
	  	  }	
	  }
}
class Customer implements Runnable {
    	Shop shop;	
    	public Customer(Shop shop) {		
    	      this.shop = shop;
    	}	
    	public void run() {	
    	  	for (int i = 1; i <= 10; i++) {			
    	  	      try {				
    	  	            this.shop.buyGoods();		
    	  	       } catch (InterruptedException e) {
    	  	            	e.printStackTrace();			
    	  	       }		
    	  	  }
   	}
}
class Shop {	
           Goods goods;
              	boolean flag;
              	// 标识商品是否充足	
              	// 生产者调用,存
          public synchronized void saveGoods(Goods goods) throws InterruptedException {		
          // 1.判断商品充足	
          	if (flag == true) {		
          		System.out.println("商品充足");		
          			this.wait();
          		// 商品充足,进入等待	
         	}	
         		// 商品不充足,生产商品,存到商场里	
         		System.out.println(Thread.currentThread().getName() + "生成并在商场里存放了" + goods.getId() + "件商品");	
         			this.goods = goods;	
         			flag = true;		
         			this.notifyAll();
         			// 将等待队列的消费者唤醒
         			}
         			// 消费者调用,取	
         	public synchronized void buyGoods() throws InterruptedException {		
         	      if (flag == false) {
         	      // 没有商品			
         	          System.out.println("商品不充足");			
         	          this.wait();
         	      // 进入等待队列,等待唤醒		
         	     }		
         	     // 正常购买商品
         	   		System.out.println(Thread.currentThread().getName() + "购买了" + goods.getId() + "件商品");		
         	   		this.goods = null;		
         	   		flag = false;		
         	   // 唤醒生产者		
         	        this.notifyAll();	
            }
  }
  class Goods {	
            private int id;	
            public Goods(int i) {	
               	this.id = i;	
           }	
        public int getId() {	
          	return id;
      	}
       	public void setId(int id) {		
       	     this.id = id;
      	}
   }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值