Java的多线程实现方式有两种,一种是继承Thread类,一中是实现Runnable接口,这两种都要重写run方法,因为在run方法中存放的是要在多线程执行的代码,使用synchronized时要绑定一个对象,对于存在多个线程竞争的程序时就需要多个程序使用的是同一个锁资源,否则无法实现同步,具体的代码如下
下面以经典的生产者、消费者为例
生产者线程:
public class Producer implements Runnable {
private LinkedList<Student> mylist=new LinkedList<Student>();
private int MAX=100;
public Producer(){
}
public Producer(LinkedList<Student> list){
this.mylist=list;