Thread advanced. MD

Source: Internet
Author: User

Examples, philosophers dine:

Sitting on a table of five philosophers, but only five chopsticks, each philosopher has only one chopsticks, so when a philosopher to pinch a dish to use the philosopher's chopsticks next to him, the philosopher who took the chopsticks can only wait for the philosopher to eat and put down his chopsticks before he can clip vegetables.

: Design ideas: First write a chopstick class, each chopsticks are an object, this class has picked up, put down two methods; When a philosopher thread calls to pick up a method, the next philosopher threads will go into the wait state, then the philosopher thread calls down the method and activates the waiting philosopher thread, In this cycle, use chopsticks in turn.

code example:

Then design the philosopher class, which inherits the thread class, and each philosopher is a thread object. This class has three attributes, which are used to hold the constructor's arguments, and the constructor of this class requires the passing of three parameters, namely the philosopher's name, the right chopstick, and the left chopstick. The type of chopsticks is the type of chopsticks, that is, the need to pass two chopsticks object. Then write and pick up and drop the method, the two method calls the chopsticks class pick up, put down method. Use the dead loop call in the Run method to pick up and drop the method, and use sleep to set a time interval.

code example:

Finally, the test class is written to build five chopsticks objects in the test class. Then constructs five philosopher objects, passes the corresponding parameter respectively. The Philosopher object is then started using the Start method.

code example:

Operation Result:

Upgrade This example to a graphical version: first build the interface, the JPanel background color is set to white: chopsticks class does not need to change, the Philosopher's class needs to add a JPanel type of property to hold the constructor parameters, the constructor plus a jpanel type of parameters. Then set JPanel in the pick up method with a red background color. In the drop-down method, set back to white.

code example:

The test class adds a thread inner class to invoke the main method of the window class.

code example:

Operation Result:

Timer Class: Timer Timer class is a tool class related to threading, as the name implies, this class can run some code at set time, which is written in the Run method. When using this class, you need to inherit the TimerTask class and use the Timer object to invoke the schedule method to implement timing.

code example:

Thread advanced. MD

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.