23,404
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
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();
}
}
}
}
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();
}
}