Java thread collaboration wait () and policyall (), waitpolicyall

Source: Internet
Author: User

Java thread collaboration wait () and policyall (), waitpolicyall

Package org. rui. thread. block; import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. timeUnit; // wax electrical automatic mode public class WaxOMatic {public static void main (String [] args) throws InterruptedException {Car car = new Car (); executorService exec = executors.newcachedthreadpool(commandid exec.exe cute (new waxoff(car###exec.exe cute (new WaxOn (car); TimeUnit. SECO NDS. sleep (5); exec. shutdownNow (); // Interrupt all tasks // shutdownNow tries to stop all ongoing active tasks and pause processing pending tasks, and return the list of tasks waiting for execution} class Car {// indicates the polishing and waxing processing status private boolean waxOn = false; // wax-mounted public synchronized void waxed () {waxOn = true; // ready to buffnotifyAll ();} // polishing public synchronized void buffed () {waxOn = false; // ready to another coat of waxnotifyAll ();} // wait wax public synchronized void waitForWaxing () throws Interrup TedException {while (waxOn = false) {wait (); // suspend this task} // wait polishing public synchronized void waitForBuffing () throws InterruptedException {while (waxOn = true) {wait (); // suspend this task }}} class WaxOn implements Runnable {private Car car; public WaxOn (Car c) {car = c ;}@ Overridepublic void run () {try {while (! Thread. interrupted () {System. out. println ("wax on! "); TimeUnit. MILLISECONDS. sleep (200); car. waxed (); // wax the car. waitForBuffing (); // wait for polishing} catch (InterruptedException e) {System. out. println ("exit through interruption"); // e. printStackTrace ();} System. out. println ("ending Wax on task ");}} ///////////// // class WaxOff implements Runnable {private Car car; public WaxOff (Car c) {car = c ;}@ Overridepublic void run () {try {while (! Thread. interrupted () {car. waitForWaxing (); // wait for the wax System. out. println ("wax off! "); TimeUnit. MILLISECONDS. sleep (200); car. buffed (); // polishing} catch (InterruptedException e) {System. out. println ("exit through interruption"); // e. printStackTrace ();} System. out. println ("ending Wax Off task") ;}/ * output :( 95% match) wax on! Wax off! Wax on! Wax off! Wax on! Wax off! Wax on! Wax off! Wax on! Wax off! Wax on! Wax off! Wax on! Wax off! Wax on! Wax off! Wax on! Exit ending Wax on task through interruption exit ending Wax Off task */





In Java, what is the difference between wait () and notify () in thread-related functions? Is it that this thread is stopped and other threads are executed?

You should not ask the difference between wait () and Policy. Wait () is to make a thread enter the waiting state, and Y () or notifyall () is to wake up the waiting thread and make it continue to run.
I suppose you want to ask: The difference between wait () and sleep (long n.
The main difference is that wait () enables the thread to release resources and then stay in the waiting state, while sleep () only allows the thread to sleep at the specified time n, without releasing the resources it occupies.

For JAVA thread problems, wait () is used; notifyAll () is used; how can I not wake up cainiao to find the best code?

The landlord, you wrote an error. Your thread has no loops. Once the execution is completed, it will end. Of course, it will not continue to be executed. Change it to the following: (add A while LOOP)
Public class Work_4_2 {

Public static void main (String [] args ){

Ticket myTicket = new Ticket ();

New Thread (myTicket). start ();
New Thread (myTicket). start ();
New Thread (myTicket). start ();
New Thread (myTicket). start ();
New Thread (myTicket). start ();

}

}

Class Ticket implements Runnable {
Static int a = 100;

Public void run (){

Synchronized (this ){

While (true ){
If (a <0 ){

Try {
Wait ();

} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
System. out. println (--);
Policyall ();
}
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.