`

马士兵_Java_复习总结---线程[下]

 
阅读更多

重写run方法是应该注意:

 无论是继承Thread类的run方法还是实现Runnable接口的run方法,都不能抛出任何异常。原因是:重写方法抛出异常的范围不能大于被重写方法抛出的异常范围。

 

线程的让步:


 

线程的合并,也叫串行化:

 

小例子:

 

 

线程的同步:

 

 

 synchronized修饰方法时,其他线程将不能锁定当前线程已经锁定的this对象

 

 

nodify方法和wait方法的小例子:


 

 

 

 

 

 

补充sleep方法与wait方法的区别



 小例子:

public class TT implements Runnable {
	int b = 100;
	
	public synchronized void m1() throws Exception{
		b = 1000;
		System.out.println("t.start线程锁定当前对象并开始陷入睡眠");
		Thread.sleep(5000);
		System.out.println("t.start线程---->b = " + b);
		System.out.println("t.start线程即将释放对对象的锁定");
	}
	
	public synchronized void m2() throws Exception {
		System.out.println("主线程锁住当前对象并开始陷入睡眠");
		Thread.sleep(2500);
		b = 2000;
		System.out.println("主线程即将释放对对象的锁定,此时的b="+b);
	}
	
	public void run() {
		try {
			System.out.println("t.start线程开始");
			m1();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws Exception {
		TT tt = new TT();
		Thread t = new Thread(tt);
		t.start();
		
		tt.m2();
		System.out.println("主线程中输出b:"+tt.b);
	}
}

 两次运行,有可能得到的两个不同的结果

 很有趣的结果,可以自己分析下。

 

死锁

public class TestDeadLock implements Runnable {
	public int flag = 1;
	static Object o1 = new Object(), o2 = new Object();
	public void run() {
	System.out.println("flag=" + flag);
		if(flag == 1) {
			synchronized(o1) {
				System.out.println("o1对象被线程1锁住!");
				try {
					Thread.sleep(500);//放大结果
				} catch (Exception e) {
					e.printStackTrace();
				}
				synchronized(o2) {
					System.out.println("线程1要拿到对对象o2的锁定,执行完此语句方法才能释放对o1对象的锁定!");	
				}
			}
		}
		if(flag == 0) {
			synchronized(o2) {
				System.out.println("o2对象被线程2锁住!");
				try {
					Thread.sleep(500);
				} catch (Exception e) {
					e.printStackTrace();
				}
				synchronized(o1) {
					System.out.println("线程2要拿到对对象o1的锁定,执行完此语句方法才能释放对o2对象的锁定!");	
				}
			}
		}
	}	
	
	public static void main(String[] args) {
		TestDeadLock td1 = new TestDeadLock();
		TestDeadLock td2 = new TestDeadLock();
		td1.flag = 1;
		td2.flag = 0;
		Thread t1 = new Thread(td1);
		Thread t2 = new Thread(td2);
		t1.start();
		t2.start();
		
	}
}

 cmd下死锁结果图

 

 

重头戏,生产消费的问题

问题:生产馒头的人不断的往篮子里面放入生产出来的馒头,吃货则不断的从篮子里面那馒头填肚子。

分析:

          先确定  生产者  消费者  馒头  装馒头的篮子 这几个对象类。对馒头类,每个窝头加个标记id,对篮子类来说,有 往里面生产 和 往外面消费 这两个方法、对生产者以及消费者这两个线程类来说,都要拿到篮子这个类对象那个的引用,而后重写各自的run方法即可。

           这里的重点就是 两个线程的同步,记住  synchronized修饰方法时,其他线程将不能锁定当前线程已经锁定的this对象

 

还是马士兵老师讲的透彻,呵呵

public class ProducerConsumer {
	public static void main(String[] args) {
		SyncStack ss = new SyncStack();
		Producer p = new Producer(ss);
		Consumer c = new Consumer(ss);
		new Thread(p).start();
//		new Thread(p).start();
//		new Thread(p).start();
		new Thread(c).start();
	}
}

class WoTou {
	int id; 
	WoTou(int id) {
		this.id = id;
	}

	public String toString() {
		return "WoTou : " + id;
	}
}

class SyncStack {
	int index = 0;
	WoTou[] arrWT = new WoTou[6];
	
	public synchronized void push(WoTou wt) {
		System.out.println("生产线程锁定当前对象,调用push方法开始生产了!" );
		while(index == arrWT.length) {
			try {
				System.out.println("生产线程因为馒头个数封顶而将陷入等待!" );
				this.wait();
				System.out.println("生产线程被唤醒!" );
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notifyAll();
		arrWT[index] = wt;
		index ++;
		System.out.println("生产线程已经生产一个!" );
	}
	
	public synchronized WoTou pop() {
		System.out.println("消费线程锁定当前对象,调用pop方法开始消费了!" );
		while(index == 0) {
			try {
				System.out.println("消费线程因为馒头个数为零而将陷入等待!" );
				this.wait();
				System.out.println("消费线程被唤醒!" );
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notifyAll();
		index--;
		System.out.println("消费线程已经消费一个!" );
		return arrWT[index];
	}
}

class Producer implements Runnable {
	SyncStack ss = null;
	Producer(SyncStack ss) {
		this.ss = ss;
	}
	
	public void run() {
		System.out.println("生产线程开始生产!");
		for(int i=0; i<20; i++) {
			WoTou wt = new WoTou(i);
			ss.push(wt);
			System.out.println("生产线程刚生产了馒头:" + wt);
			try {
				Thread.sleep((int)(Math.random() * 200));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}			
		}
	}
}

class Consumer implements Runnable {
	SyncStack ss = null;
	Consumer(SyncStack ss) {
		this.ss = ss;
	}
	
	public void run() {
		System.out.println("消费线程开始消费!");
		for(int i=0; i<20; i++) {
			WoTou wt = ss.pop();
			System.out.println("消费刚吃掉了馒头: " + wt);
			try {
				Thread.sleep((int)(Math.random() * 1000));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}			
		}
	}
}

 自己运行结果分析,我加了写便于理解程序运行到何种地步的代码,希望能帮到大家理解

 

 

 

欢迎光临我师傅小锋哥的网站   Java知识分享网  www.java1234.com

    有大量Java资料供学习参考用,一切免费

 

 

  • 大小: 34.1 KB
  • 大小: 34.4 KB
  • 大小: 33.8 KB
  • 大小: 91.9 KB
  • 大小: 15.1 KB
  • 大小: 158.4 KB
  • 大小: 56.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics