28.5. JScrollPane

 
[Page 809]

Chapter Summary

  • Each task is an instance of the Runnable interface. A thread is an object that facilitates the execution of a task. You can declare a task class by implementing the Runnable interface and create a thread by wrapping a task using a Thread constructor.

  • After a thread object is created, use the start() method to start a thread, and the sleep(long) method to put a thread to sleep so that other threads get a chance to run.

  • A thread object never directly invokes the run method. The JVM invokes the run method when it is time to execute the thread. Your class must override the run method to tell the system what the thread will do when it runs.

  • To prevent threads from corrupting a shared resource, use synchronized methods or blocks. A synchronized method acquires a lock before it executes. In the case of an instance method, the lock is on the object for which the method was invoked. In the case of a static (class) method, the lock is on the class.

  • A synchronized statement can be used to acquire a lock on any object, not just this object, when executing a block of the code in a method. This block is referred to as a synchronized block .

  • You can use explicit locks and conditions to facilitate the communications among threads, as well as using the built-in monitor for objects.

  • Deadlock occurs when two or more threads acquire locks on multiple objects and each has a lock on one object and is waiting for the lock on the other object. The resource ordering technique can be used to avoid deadlock.

  • You can use a JProgressBar to track the progress of a thread.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net