1. 程式人生 > >執行緒同步基礎——synchronized關鍵字

執行緒同步基礎——synchronized關鍵字

synchronized關鍵字:可用於修飾方法和程式碼塊,我這裡用經典的例子——消費者與生產者

  1. 修飾程式碼塊來實現
/**
 * 生產者
 * 
 * @author Administrator
 * 
 */
public class Producer implements Runnable {
	private int max = 10;
	private LinkedList<String> list;

	@Override
	public void run() {
		while (true) {
			synchronized (list) {
				while (list.size() >= max) {
					try {
						this.list.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				list.add("測試");
				System.out.println(Thread.currentThread().getName() + "生產測試");
				this.list.notifyAll();
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	public LinkedList<String> getList() {
		return list;
	}

	public void setList(LinkedList<String> list) {
		this.list = list;
	}

	public Producer(LinkedList<String> list) {
		super();
		this.list = list;
	}

}

/**
 * 消費者
 * 
 * @author Administrator
 * 
 */
public class Consumer implements Runnable {
	private LinkedList<String> LinkedList;

	public LinkedList<String> getLinkedList() {
		return LinkedList;
	}

	public void setLinkedList(LinkedList<String> LinkedList) {
		this.LinkedList = LinkedList;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (LinkedList) {
				while (LinkedList.size() <= 0) {
					try {
						this.LinkedList.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				LinkedList.poll();
				System.out.println(Thread.currentThread().getName() + "消費測試");
				this.LinkedList.notifyAll();
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public Consumer(LinkedList<String> LinkedList) {
		super();
		this.LinkedList = LinkedList;
	}

}

/**
 * 測試類
 * @author Administrator
 *
 */
public class Test {
	public static void main(String[] args) {
		LinkedList<String> list = new LinkedList<String>();
		Producer producer1 = new Producer(list);
		Producer producer2 = new Producer(list);
		Thread thread1 = new Thread(producer1);
		Thread thread2 = new Thread(producer2);
		
		Consumer consumer1 = new Consumer(list);
		Thread thread3 = new Thread(consumer1);
		
		Consumer consumer2 = new Consumer(list);
		Thread thread4 = new Thread(consumer2);
		
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
	}
}

  1. 修飾方法來實現
/**
 * 生產者
 * 
 * @author Administrator
 * 
 */
public class Producer implements Runnable {
	private Storage storage;

	@Override
	public void run() {
		while (true) {
			storage.setData();
		}

	}

	public Storage getStorage() {
		return storage;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}

	public Producer(Storage storage) {
		super();
		this.storage = storage;
	}

}

/**
 * 消費者
 * 
 * @author Administrator
 * 
 */
public class Consumer implements Runnable {
	private Storage storage;

	@Override
	public void run() {
		while (true) {
			storage.getData();
		}
	}

	public Storage getStorage() {
		return storage;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}

	public Consumer(Storage storage) {
		super();
		this.storage = storage;
	}

	public Consumer() {
		super();
	}

}

/**
 * 操作儲存物件
 * @author Administrator
 *
 */
public class Storage {
	private int max;
	private LinkedList<String> list;

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public LinkedList<String> getList() {
		return list;
	}

	public void setList(LinkedList<String> list) {
		this.list = list;
	}

	public Storage(int max, LinkedList<String> list) {
		super();
		this.max = max;
		this.list = list;
	}

	public synchronized void getData() {
		while (list.size() <= 0) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		list.add(Thread.currentThread().getName()+"消費測試");
		list.poll();
		notifyAll();
	}
	
	public synchronized void setData(){
		while (list.size() >= max) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("生產測試");
		list.add(Thread.currentThread().getName()+"測試");
		notifyAll();
	}
}

/**
 * 測試類
 * @author Administrator
 *
 */
public class Test {
	public static void main(String[] args) {
		Storage storage = new Storage(10, new LinkedList<String>());
		Producer producer1 = new Producer(storage);
		Producer producer2 = new Producer(storage);
		Thread thread1 = new Thread(producer1);
		Thread thread2 = new Thread(producer2);
		
		Consumer consumer1 = new Consumer(storage);
		Thread thread3 = new Thread(consumer1);
		
		Consumer consumer2 = new Consumer(storage);
		Thread thread4 = new Thread(consumer2);
		
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
	}
}

注:注意執行緒執行過程中的迴圈,以及wait與notifyAll的具體含義,弄清wait與sleep的區別