Java Thread Exercises

Source: Internet
Author: User
Tags sleep thread thread class

(1) Inherit a class from thread, and (overload) overwrite the Run () method. Within run (), print out a message and call sleep (). Repeat these actions three times, and then return from Run (). Place a startup message in the builder and overwrite finalize () to print a closing message. Create a separate thread class that calls System.GC () and System.runfinalization () within run () and prints a message indicating that the call was successful. Create these two types of threads, and then run them to see what happens.
(2) Modify the Counter2.java to make the thread an internal class and do not need to explicitly save one that points to Counter2.
(3) Modify the Sharing2.java and add a synchronized (synchronized) block inside the Twocounter run () method instead of synchronizing the entire run () method.
(4) Create two thread subclasses, the first run () method is used for the start start, and the handle of the second thread object is captured, and then the Wait () is invoked. Run () of the second class should call Modifyall () for the first thread in a few seconds, so that the first thread can print out a message.
(5) in the Ticker2 Counter5.java, delete the yield () and explain the result. Replace yield () with a sleep () and explain the results.
(6) In Threadgroup1.java, replace the call to Sys.suspend () with a wait () call to the thread group for 2 seconds. In order to obtain the correct result, the object lock of Sys must be obtained within a synchronized block.
(7) Modify the Daemons.java so that main () has a sleep () instead of a readline (). Experiment with different sleep times and see what happens.
(8) to the 7th chapter (the middle section) to find the Greenhousecontrols.java example, it should be composed of three documents. In Event.java, the event class is built on the monitoring of time. Modify this event to make it a thread. The rest of the design is then modified so that they work in concert with the new, thread-based event.

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.