synchronized关键字:可用于修饰方法和代码块,我这里用经典的例子——消费者与生产者
- 修饰代码块来实现
/**
* 生产者
*
* @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();
}
}
- 修饰方法来实现
/**
* 生产者
*
* @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的区别