package cn. campsg. java. experiment. entity ;
import java. util. ArrayList ;
import java. util. List ;
public class SharePool {
private List < String > pool= new ArrayList ( ) ;
private final int MAX= 15 ;
public List < String > getPool ( ) { return pool; }
public void setPool ( List < String > pool) { this . pool = pool; }
public SharePool ( ) {
super ( ) ;
}
public void produce ( String infor) {
synchronized ( this ) {
if ( pool. size ( ) == MAX) {
try {
wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System . out. println ( "订阅请求队列已满,等待系统处理订阅请求中......" ) ;
}
else {
pool. add ( infor) ;
System . out. println ( "订阅者@" + Thread . currentThread ( ) . getName ( ) + ":订阅《" + infor + "》申请已提交.当前订阅数量为:" + pool. size ( ) ) ;
try {
notify ( ) ;
}
catch ( Exception e )
{
e. printStackTrace ( ) ;
}
}
}
}
public void consume ( ) {
synchronized ( this ) {
if ( pool. size ( ) == 0 ) {
try {
wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System . out. println ( "处理者@" + Thread . currentThread ( ) . getName ( ) + "暂无订阅请求信息,等待中......" ) ;
}
else {
String infor = pool. remove ( 0 ) ;
System . out. println ( "处理者@" + Thread . currentThread ( ) . getName ( ) + ":处理《" + infor + "》订阅已完毕。尚待处理订阅数量为:" + pool. size ( ) ) ;
try {
notify ( ) ;
}
catch ( Exception e)
{
e. printStackTrace ( ) ;
}
}
}
}
}
package cn. campsg. java. experiment. entity ;
public class Producer implements Runnable {
private SharePool pool;
private int count;
public Producer ( ) { super ( ) ; }
public Producer ( SharePool pool, int count) {
super ( ) ;
this . pool = pool;
this . count = count;
}
public void run ( ) {
String infor = "报刊杂志" ;
int counts = 0 ;
System . out. println ( "订阅者@" + Thread . currentThread ( ) . getName ( ) + ":订阅" + count + "份。" ) ;
while ( count > 0 ) {
counts++ ;
System . out. println ( "订阅者@" + Thread . currentThread ( ) . getName ( ) + " 在提交第" + counts + " 份订阅申请。" ) ;
pool. produce ( infor + counts) ;
count-- ;
try {
Thread . sleep ( 500 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
System . out. println ( "订阅者@" + Thread . currentThread ( ) . getName ( ) + "->完成订阅。" ) ;
}
}
package cn. campsg. java. experiment. entity ;
public class Consumer implements Runnable {
private SharePool pool;
public Consumer ( ) {
super ( ) ;
}
public Consumer ( SharePool pool) {
super ( ) ;
this . pool = pool;
}
public void run ( ) {
int counts = 1 ;
while ( true )
{
if ( counts <= 15 ) {
System . out. println ( "处理者@" + Thread . currentThread ( ) . getName ( ) + ":处理第" + counts + "份订阅。" ) ;
this . pool. consume ( ) ;
} else
{
System . out. println ( "本线程完成订阅处理量,即刻退出。处理者@" + Thread . currentThread ( ) . getName ( ) ) ;
break ;
}
counts++ ;
}
}
}
package cn. campsg. java. experiment ;
import cn. campsg. java. experiment. entity. Consumer ;
import cn. campsg. java. experiment. entity. Producer ;
import cn. campsg. java. experiment. entity. SharePool ;
public class MainClass {
public static void main ( String [ ] args) {
SharePool pool = new SharePool ( ) ;
for ( int i = 1 ; i <= 5 ; i++ ) {
new Thread ( new Producer ( pool, i) ) . start ( ) ;
}
new Thread ( new Consumer ( pool) ) . start ( ) ;
}
}