/** the thread number of consumer **/
private static final int consumerNumber = 10;
/** the consumer Thread Name prefix **/
private static final String consumerThreadName = "consumer_notify";
/** the consumer thread index that will be notified **/
private static final int index = 5;
/** the list register all the consumer list **/
private static final List<Thread> registeThreads = new ArrayList<Thread>(
consumerNumber);
for (int i = 1; i <= consumerNumber; i++) {
final Thread t = new ConsumerThread();
t.setName(consumerThreadName + i);
registeThreads.add(t);
}
for (final Thread t : registeThreads) {
t.start();
}
new ProducerThread().join();
}
/**
* Consumer synchronize the current instance itself
*
* @author Administrator
*
*/
static class ConsumerThread extends Thread {
@Override
public void run() {
final Thread t = Thread.currentThread();
final ThreadLocal<Boolean> isWait = new ThreadLocal<Boolean>();
isWait.set(true);
synchronized (this) {
while (isWait.get()) {
try {
System.out.println("i am ready to wait,"
+ " the thread name is:" + t.getName());
this.wait();
isWait.set(false);
} catch (InterruptedException ex) {
System.err.println(ex.getMessage());
}
}
System.out.println("i have been released, the thread name is:"
+ t.getName());
}
}
}
/**
* the Producer will synchronize consumer instance as thread monitor
*
* @author Administrator
*
*/
static class ProducerThread extends Thread {
public ProducerThread() {
this.start();
}
@Override
public void run() {
for (Thread consumer : registeThreads) {
final String consumerThreadName = consumer.getName();
if (consumerThreadName.equalsIgnoreCase(consumerThreadName
+ index)) {
synchronized (consumer) {
consumer.notifyAll();
}
}
/** the thread number of consumer **/
private static final int consumerNumber = 10;
/** the consumer Thread Name prefix **/
private static final String consumerThreadName = "consumer_notify";
/** the consumer thread index that will be notified **/
private static final int index = 5;
/** the list register all the consumer list **/
private static final List<Thread> registeThreads = new ArrayList<Thread>(
consumerNumber);
for (int i = 1; i <= consumerNumber; i++) {
final Thread t = new ConsumerThread();
t.setName(consumerThreadName + i);
registeThreads.add(t);
}
for (final Thread t : registeThreads) {
t.start();
}
new ProducerThread().join();
}
/**
* Consumer synchronize the current instance itself
*
* @author Administrator
*
*/
static class ConsumerThread extends Thread {
@Override
public void run() {
final Thread t = Thread.currentThread();
final ThreadLocal<Boolean> isWait = new ThreadLocal<Boolean>();
isWait.set(true);
synchronized (this) {
while (isWait.get()) {
try {
System.out.println("i am ready to wait,"
+ " the thread name is:" + t.getName());
this.wait();
isWait.set(false);
} catch (InterruptedException ex) {
System.err.println(ex.getMessage());
}
}
System.out.println("i have been released, the thread name is:"
+ t.getName());
}
}
}
/**
* the Producer will synchronize consumer instance as thread monitor
*
* @author Administrator
*
*/
static class ProducerThread extends Thread {
public ProducerThread() {
this.start();
}
@Override
public void run() {
for (Thread consumer : registeThreads) {
final String name = consumer.getName();
if (name.equalsIgnoreCase(consumerThreadName + index)) {
synchronized (consumer) {
consumer.notifyAll();
}
}
/** the thread number of consumer **/
private static final int consumerNumber = 10;
/** the consumer Thread Name prefix **/
private static final String consumerThreadName = "consumer_notify";
/** the consumer thread index that will be notified **/
private static final int index = 6;
/** the list register all the consumer list **/
private static final List<Thread> registeThreads = new ArrayList<Thread>(
consumerNumber);
Slot s = new Slot();
for (int i = 1; i <= consumerNumber; i++) {
final Thread t = new ConsumerThread(s);
t.setName(consumerThreadName + i);
registeThreads.add(t);
}
for (final Thread t : registeThreads) {
t.start();
}
new ProducerThread(s).join();
}
/**
* Consumer synchronize the current instance itself
*
* @author Administrator
*
*/
static class ConsumerThread extends Thread {
Slot slot ;
ConsumerThread(Slot s){
slot = s ;
}
@Override
public void run() {
final Thread t = Thread.currentThread();
final ThreadLocal<Boolean> isWait = new ThreadLocal<Boolean>();
isWait.set(true);
synchronized (this) {
while (slot.slotNum==0) {
try {
System.out.println("i am ready to wait,"
+ " the thread name is:" + t.getName());
this.wait();
/** the thread number of consumer **/
private static final int consumerNumber = 10;
/** the consumer Thread Name prefix **/
private static final String consumerThreadName = "consumer_notify";
/** the consumer thread index that will be notified **/
private static final int index = 6;
/** the list register all the consumer list **/
private static final List<Thread> registeThreads = new ArrayList<Thread>(
consumerNumber);
Slot s = new Slot();
for (int i = 1; i <= consumerNumber; i++) {
final Thread t = new ConsumerThread(s);
t.setName(consumerThreadName + i);
registeThreads.add(t);
}
for (final Thread t : registeThreads) {
t.start();
}
new ProducerThread(s).join();
}
/**
* Consumer synchronize the current instance itself
*
* @author Administrator
*
*/
static class ConsumerThread extends Thread {
Slot slot ;
ConsumerThread(Slot s){
slot = s ;
}
@Override
public void run() {
final Thread t = Thread.currentThread();
synchronized (this) {
while (slot.slotNum==0) {
try {
System.out.println("i am ready to wait,"
+ " the thread name is:" + t.getName());
this.wait();
i am ready to wait, the thread name is:consumer_notify1
i am ready to wait, the thread name is:consumer_notify4
i am ready to wait, the thread name is:consumer_notify6
i am ready to wait, the thread name is:consumer_notify2
i am ready to wait, the thread name is:consumer_notify3
i am ready to wait, the thread name is:consumer_notify5
i am ready to wait, the thread name is:consumer_notify7
i am ready to wait, the thread name is:consumer_notify8
i am ready to wait, the thread name is:consumer_notify9
i am ready to wait, the thread name is:consumer_notify10
i am ready to wait, the thread name is:consumer_notify6