千锋20200312

在千锋“逆战”学习第29天

      每日一句:如果你没有把握做到,最好就不要承诺,你什么也不承诺,至少别人不会看不起你。
      今天学习了线程中死锁、线程池的内容
      明天继续加油。

作业

8.有如下代码:

class MyThread extends Thread {
	private String data;

	public MyThread(String data) {
		this.data = data;
	}

	public void run() {
		for (int i = 0; i < 10; 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.run方法改为同步方法
public synchronized void run() {
			for (int i = 0; i < 10; i++) {
				System.out.println(data);
		}
	}
C.把run方法中的所有内容都放在synchornized(System.out)代码块中
public void run() {
		synchronized (System.out) {
			for (int i = 0; i < 100; i++) {
				System.out.println(data);
			}
		}
}

--------------------------------------------------------------------------
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() {
		for(int i=0;i<100;i++) throws Exception{
			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) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("hello");
		}
	}
}

class MyThread2 extends Thread{
	public void run() {
		for(int i=0;i<100;i++) /*throws Exception*/{
			try {
				//2.不能声明异常,必须手动捕获异常
				this.sleep((int)Math.random()*1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				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件商品
商品充足
消费者购买了1件商品
商品不充足
生产者生成并在商场里存放了2件商品
商品充足
消费者购买了2件商品
商品不充足
生产者生成并在商场里存放了3件商品
商品充足
消费者购买了3件商品
商品不充足
生产者生成并在商场里存放了4件商品
商品充足
消费者购买了4件商品
商品不充足
生产者生成并在商场里存放了5件商品
商品充足
消费者购买了5件商品
商品不充足
生产者生成并在商场里存放了6件商品
商品充足
消费者购买了6件商品
商品不充足
生产者生成并在商场里存放了7件商品
商品充足
消费者购买了7件商品
商品不充足
生产者生成并在商场里存放了8件商品
商品充足
消费者购买了8件商品
商品不充足
生产者生成并在商场里存放了9件商品
商品充足
消费者购买了9件商品
商品不充足
生产者生成并在商场里存放了10件商品
消费者购买了10件商品
发布了40 篇原创文章 · 获赞 0 · 访问量 1140

猜你喜欢

转载自blog.csdn.net/qq_41841482/article/details/104827537
今日推荐