concurrent包学习demo2--备忘

luozhangwen 2010-07-26 04:45:23

package com.lzw.test;

import java.util.concurrent.*;

public class ObjectThread
{

public static void main(String[] args)
{
ExecutorService service = Executors.newCachedThreadPool();
final Business1 bussiness = new Business1();
service.execute(new Runnable()
{
public void run()
{
while(true)
bussiness.main();
}
});
service.execute(new Runnable()
{
public void run()
{
while(true)
bussiness.sub();
}
});
}
}


class Business1
{
private boolean isShouldSub = false; //是否可以sub


public synchronized void main()
{
if (isShouldSub == true)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

for (int i = 10; i <= 15; i++)
{
try
{
Thread.sleep(300);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " " + i);
}
isShouldSub = true;
this.notify();
}


public synchronized void sub()
{

if (isShouldSub == false)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

for (int i = 1; i <= 5; i++)
{
try
{
Thread.sleep(300);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " " + i);
}
isShouldSub = false;
this.notify();
}

}

...全文
61 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
luozhangwen 2010-07-29
  • 打赏
  • 举报
回复

package com.lzw.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;

public class BlockingQueueTest
{
public static void main(String[] args) throws InterruptedException
{

BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<String>();
Producer producer = new Producer(blockingQueue);

Consumer consumer1 = new Consumer(blockingQueue,"1号消费者");
Consumer consumer2 = new Consumer(blockingQueue,"2号消费者");

new Thread(producer).start();
new Thread(consumer1).start();
new Thread(consumer2).start();
}
}


class Producer implements Runnable
{
private BlockingQueue<String> queue = null;

public Producer(BlockingQueue<String> queue)
{
this.queue = queue;
}


public void run()
{
int i = 0;
while (true)
{
i++;
try
{
Thread.sleep(new Random().nextInt(1000));
System.out.println("生产----" +i );
queue.put(String.valueOf(i));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}


class Consumer implements Runnable
{
private BlockingQueue<String> queue = null;
private String name = null;

public Consumer(BlockingQueue<String> queue,String name)
{
this.queue = queue;
this.name = name;
}


public void run()
{
while (true)
{
try
{
Thread.sleep(new Random().nextInt(1000));
System.out.println(name + "消费----"+queue.take());
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
luozhangwen 2010-07-26
  • 打赏
  • 举报
回复

package com.lzw.test;

import java.util.concurrent.*;
import java.util.concurrent.locks.*;

public class LockCondition
{
public static void main(String[] args)
{
ExecutorService service = Executors.newCachedThreadPool();
final Business2 business2 = new Business2();

service.execute(new Runnable()
{
public void run()
{
while(true)
business2.main();
}
});

service.execute(new Runnable()
{
public void run()
{
while(true)
business2.sub();
}
});
}
}


class Business2
{
private boolean isShouldSub = true;

Lock lock = new ReentrantLock();

Condition condition = lock.newCondition();


public void main()
{
lock.lock();

if (isShouldSub == true)
{
try
{
condition.await();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

for (int i = 10; i <= 15; i++)
{
try
{
Thread.sleep(300);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " " + i);
}

isShouldSub = true;
condition.signal();
lock.unlock();
}


public void sub()
{
lock.lock();

if (isShouldSub == false)
{
try
{
condition.await();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

for (int i = 1; i <= 5; i++)
{
try
{
Thread.sleep(300);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " " + i);
}

isShouldSub = false;
condition.signal();
lock.unlock();
}

}

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧