java线程两种实现方式:第一个继承方式--继承thread类,另一个是实现Runnerable接口
今天讨论的是关于实现Runnerable接口的一个经典案例
1.共享资源:实现了三种版本的共享资源。
//共享资源 public class PeopleShareResource { private String name; private Date date; // //第一版 出现重复消费和重复生产 // //1存入 // public void setResource(String name,Date date) { // this.name=name; // try { // Thread.sleep(10); // } catch (InterruptedException e) { // e.printStackTrace(); // } // this.date=date; // } // //1取出 // public void getResource() { // try { // Thread.sleep(10); // } catch (InterruptedException e) { // e.printStackTrace(); // } // System.out.println("姓名:"+this.name+"---"+"日期:"+this.date); // } // // 第二版 解决出现重复消费和重复生产,加入synchronized和判断当前生产者与消费者的状态 // // // 表示检查当前共享资源状态是否为空 // private boolean isEmpty = true; // // // 2存入 // public synchronized void setResource(String name, Date date) { // // try { // // 如果为false说明不空, // while (!isEmpty) { // this.wait();// 不空时,生产者停止生产,进入等待 // } // // 如果为true,为空时,进行生产 // this.name = name; // Thread.sleep(10); // this.date = date; // // 生产完毕,设置状态为false,不空的状态 // isEmpty = false; // //唤醒消费者 // this.notifyAll(); // } catch (Exception e) { // e.getMessage(); // } // } // // // 2取出 // public synchronized void getResource() { // try { // // 状态为空, // while (isEmpty) { // this.wait();// 为空,消费者等待,生产者来生产 // } // //不空时候,消费者进行消费 // System.out.println("姓名:"+this.name+"---"+"日期:"+this.date); // //消费完毕 // isEmpty = true; // //唤醒生产者 // this.notifyAll(); // } catch (InterruptedException e) { // e.printStackTrace(); // } // } // 第三版 解决出现重复消费和重复生产,使用Lock和Condition接口 // 表示检查当前共享资源状态是否为空 private boolean isEmpty = true; private final Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); // 3存入 public void setResource(String name, Date date) { //获取锁 lock.lock(); try { // 如果为false说明不空, while (!isEmpty) { condition.await();// 生产者等待, } // 为空,进行生产 this.name = name; Thread.sleep(10); this.date = date; // 生产完毕,设置状态为false,不空的状态 isEmpty = false; //唤醒消费者 condition.signalAll(); } catch (Exception e) { e.getMessage(); }finally { lock.unlock(); } } // 3取出 public void getResource() { //获取锁 lock.lock(); try { // 状态为空, while (isEmpty) { condition.await();// 等待生产者来生产 } //不空进行消费 System.out.println("姓名:"+this.name+"---"+"日期:"+this.date); //消费完毕 isEmpty = true; //唤醒生产者 condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } }
2.生产者
public class ThreadProducter implements Runnable{ private PeopleShareResource resource; public ThreadProducter(PeopleShareResource resource) { this.resource=resource; } public void run() { for (int i = 0; i < 5000; i++) { if(i%2==0) { resource.setResource("王强", new Date()); }else{ resource.setResource("高颖", new Date()); } } }}
3.消费者
public class ThreadCoustomer implements Runnable{ private PeopleShareResource resource; public ThreadCoustomer(PeopleShareResource resource) { this.resource=resource; } public void run() { for (int i = 0; i < 5000; i++) { resource.getResource(); } }}
4.启动线程
public class ThreadArea{ public static void main(String[] args) { //共享资源 PeopleShareResource resource=new PeopleShareResource(); //生产者 ThreadCoustomer coustomer=new ThreadCoustomer(resource); //消费者 ThreadProducter producter=new ThreadProducter(resource); // public Thread(Runnable target) //Runnable target-->coustomer/producter new Thread(coustomer).start(); new Thread(producter).start(); } }