黑马程序员技术交流社区
标题:
发一个今天的练习
[打印本页]
作者:
noiary
时间:
2014-9-13 23:55
标题:
发一个今天的练习
多线程略难,时间也少. 第十二天,两天了还没吃透.
不过我想,多线程应该是java中的重点,学好这里下面才会相对轻松些...
很期待13天的String ~
/*
生产者消费者练习
synchronized版
*/
public class ProducerConsumerDemo {
public static void main(String[] args) {
Resource res = new Resource();
new Thread(new Producer(res)).start();
new Thread(new Producer(res)).start();
new Thread(new Consumer(res)).start();
new Thread(new Consumer(res)).start();
}
}
class Resource {
private String name;
private int count = 1;
private boolean flag;
public synchronized void set(String name) {
while(flag)
try{wait();} catch(Exception e) {}
this.name = name + "--" + count++;
System.out.println(Thread.currentThread().getName() + "\t生产者" + this.name);
flag = true;
notifyAll();
}
public synchronized void out() {
while(!flag)
try{wait();} catch(Exception e) {}
System.out.println(Thread.currentThread().getName() + "\t\t消费者" + this.name);
flag = false;
notifyAll();
}
}
class Producer implements Runnable {
Resource res;
public Producer(Resource res) {
this.res = res;
}
public void run() {
while(true) {
res.set("商品");
}
}
}
class Consumer implements Runnable {
Resource res;
public Consumer(Resource res) {
this.res = res;
}
public void run() {
while(true) {
try{Thread.sleep(500);} catch(Exception e) {}
res.out();
}
}
}
复制代码
import java.util.concurrent.locks.*;
/*
生产者消费者练习
ReentrantLock版
*/
public class ProducerConsumerDemo2 {
public static void main(String[] args) {
Resource res = new Resource();
new Thread(new Producer(res)).start();
new Thread(new Producer(res)).start();
new Thread(new Consumer(res)).start();
new Thread(new Consumer(res)).start();
}
}
class Resource {
private String name;
private int count = 1;
private boolean flag;
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set(String name)
throws InterruptedException {
lock.lock();
try {
if(flag)
condition_pro.await();
this.name = name + "--" + count++;
System.out.println(Thread.currentThread().getName() + "生产者" + this.name);
flag = true;
condition_con.signal();
}
finally {
lock.unlock();
}
}
public void out()
throws InterruptedException {
lock.lock();
try {
if(!flag)
condition_con.await();
System.out.println(Thread.currentThread().getName() + "\t\t消费者" + this.name);
flag = false;
condition_pro.signal();
}
finally {
lock.unlock();
}
}
}
class Producer implements Runnable {
Resource res;
public Producer(Resource res) {
this.res = res;
}
public void run() {
while(true) {
try {
res.set("商品");
}
catch(InterruptedException e) {
}
}
}
}
class Consumer implements Runnable {
Resource res;
public Consumer(Resource res) {
this.res = res;
}
public void run() {
while(true) {
try {
res.out();
}
catch(InterruptedException e) {
}
}
}
}
复制代码
作者:
fantacyleo
时间:
2014-9-14 01:12
如果能把张老师的Java并发库视频看完,并用里面的每种工具把生产者消费者问题实现一遍,你的Java多线程就算入门了
作者:
ItJack
时间:
2014-9-14 14:46
感谢分享。
作者:
noiary
时间:
2014-9-14 22:57
fantacyleo 发表于 2014-9-14 01:12
如果能把张老师的Java并发库视频看完,并用里面的每种工具把生产者消费者问题实现一遍,你的Java多线程就算 ...
好的,一定实践~!
作者:
暴走的小青春丶
时间:
2014-9-14 23:07
并发库的视频在哪里啊
作者:
不系之舟王
时间:
2014-9-15 00:24
学习一下
作者:
648947721
时间:
2014-9-15 00:55
学习学习
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2