I wrote a message in the forum today for netizens. I have already debugged one developer who corresponds to 20 consumers. The code is relatively casual. I didn't write classes separately. I used internal classes. if there is something wrong with that place, you are welcome to correct and criticize it.
Public class executorservicetest {
Private Static int maxtask = 80;
Private Static object lock = new object ();
Public static void main (string [] ARGs ){
Final list tasks = new arraylist ();
Final executorservice exec = executors. newcachedthreadpool ();
Exec.exe cute (New runnable (){
Public void run (){
While (! Thread. interrupted ()){
Try {
Synchronized (tasks ){
If (tasks. Size () = maxtask)
Tasks. Wait ();
}
Synchronized (LOCK ){
If (tasks. Size ()! = Maxtask ){
Tasks. Add (new object ());
System. Out. println ("production task ");
Lock. policyall ();
Thread. Yield ();
}
}
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
});
For (INT I = 0; I <20; I ++ ){
Exec.exe cute (New runnable (){
Public void run (){
While (! Thread. interrupted ()){
Try {
Synchronized (LOCK ){
If (tasks. Size () = 0)
Lock. Wait ();
}
Synchronized (tasks ){
If (tasks. Size ()> 0 ){
Tasks. Remove (0 );
System. Out. println ("Processing Task ");
Tasks. policyall ();
}
}
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
});
}
Exec. Shutdown ();
}
}