Thread Priorities
You can make a Java thread have a preference during execution. The priority attribute of the Thread determines this characteristic. The priority can range between 1 to 10; all threads are created with a priority of 5.
The example code defines three constants:
-
Thread.MIN_PRIORITY: This has a value of 1.
-
Thread.NORM_PRIORITY: This has a value of 5.
-
Thread.MAX_PRIORITY: This has a value of 10.
The Thread's setPriority(int value) and the getPriority() methods allow you to set and retrieve the priority value of the Thread.
Thread States
A thread can be in one of the following states:
Ready: A thread is in Ready state when it's ready for execution, but not actually being executed. A thread in this state qualifies for access to the CPU, in order to enter into the Running state.
Running: A thread is in Running state when it's being executed. This thread has access to the CPU.
Dead: A thread is in Dead state when the execution of the run method is completed. When a thread is dead, it can no longer be executed.
Waiting: In this state, the thread is waiting for an instruction. The instruction can be to release a monitor, sleeping, etc. A thread in this state qualifies to reach Ready state only after the instruction has been issued.
Synchronized Keyword
Of course, using threads does have a drawback. If a program has multiple threads, it could happen that the same code is accessed/executed by more than one thread at a given point in time. This situation can lead to data inconsistency. Luckily, the synchronized keyword helps solve this problem.
To use this keyword, place it in the signature of a method as shown in the code below. Placing a synchronized keyword in the signature of a method restricts the execution of
An object's synchronized block of code to only one thread at a time. If a thread has already started executing your synchronized block of code for an object, no other thread can start to execute the block of code for that same object until the thread in action completes the synchronized method:
public synchronized void syncMethod() {
if(! isAlreadyProcessed)
{
isAlreadyProcess = true;
//Do other activities
}
}
Use Threads Wisely
Threads are an important part of the Java programming language. They can either speed up the application to a remarkable state or slow it down if they're not implemented properly.
Though designing a multi-threaded application is most always advisable, their injudicious use can result in memory or stack issues.
Related Resources
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|