Category Archive for: The Complete Reference

Extending Thread

Extending Thread The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that lass, The extending class must override the rune I method, which is the entry point fa the new thread, It must also call start to begin execution of the new thread, Here…

Read More →

Choosing an Approach ,

Choosing an Approach   At this point, you .might be wondering why Java has two ways to create child .threads and which approach is better. The answers to these questions turn on the same point.  The Thread class defines several methods that can be overridden by a derived class. Of these methods, the only one that must be…

Read More →

Creating Multiple Threads

Creating Multiple Threads So far, you have been using only two threads the main thread and. one child thread. However, your program can spawn as many threads as it needs. For example, the following program creates three child threads: The output from-this program is shown here: New thread:                  Thread…

Read More →

Using is Alive ( )and Join ( )

Using is Alive ( )and Join ( ) As mentioned, the main thread must be the last thread to finish. In the preceding examples, this is accomplished by calling sleep( ) within maine ), with a long enough delay to ensure that all child threads terminate prior to the main thread. However, this is hardly a satisfactory solution,…

Read More →

Thread priorities

Thread priorities   Thread priorities are used by the thread scheduler to decide when each thread should be allowed to run. In theory, higher-priority threads get more CPU time than lower-priority threads. In practice, the amount of CPU time that a thread gets often depends on several factors besides-its priority. (For example, how an operating system implements multitasking can affect…

Read More →

Synchronization

Synchronization  When two or more threads need access to .1 shored resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called  As you will see, Java provides unique, language-level support for it. . Kcy to synchronization is the concept…

Read More →

Using Synchronized Methods

Using -Synchronized Methods  Synchronization is easy in Java, because all objects have their on implicit monitor associated with them. To enter an object’s monitor, just call a method that has been modified with the synchronized keyword. Whiic a thread is inside a synchronized method, all other threads that try to call it (or any other synchronized method) on…

Read More →

The synchronized Statement

The synchronized Statement  While creating synchronized methods within classes that you create is an easy and effective means of achieving synchronization, it will not work in all cases. To understand why, consider the following. Imagine that you want to synchronize access to objects of a class that was not ~signed for multithreaded access. That is, the class. does not…

Read More →

Inter thread Communication

Inter thread Communication The preceding examples unconditionally blocked other threads from asynchronous access to certain methods. This use of the implicit monitors in Java objects is powerful. but you can achieve a more subtle level of control through interprocess communication. As you will see, this is especially easy in Java. As discussed earlier, multithreading replaces event loop programming by…

Read More →

Deadlock

Deadlock A special type of error that you need to avoid that relates ‘specifically to multitasking is deadlock, which occurs when two threads have a circular dependency on a pair of synchronized objects. For example, suppose one thread enters the monitor on object X and another thread enters the monitor on object Y.lf the thread in X tries…

Read More →

Back to Top