advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : An Introduction to Multi-threading :

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:

  1. Thread.MIN_PRIORITY: This has a value of 1.
  2. Thread.NORM_PRIORITY: This has a value of 5.
  3. 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

How to Add Java Applets to Your Site

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.

 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 38
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
Jim Zemlin: The New Center of Linux Gravity
Microsoft's Novell Investment Tops $340M
Fedora 10 Takes Shape
IBM Gives a Mobile Voice to Developers
Inadequate Tools Send Software Down the Drain
USB 3.0 One Step Closer to Reality
Would-Be Linux Contributors May Get a Leg Up

Develop a Mobile RSS Feed the Easy Way
State of the Semantic Web: Know Where to Look
A 3D Exploration of the HTML Canvas Element
Setting Up and Running Subversion and Tortoise SVN with Visual Studio and .NET
Java/JRuby Developers, Say Open 'Sesame' to the Semantic Web
Interpreting Images with MRDS Services
DevXtra Editors' Blog: Executives Avoiding Cloud Computing in Droves
Q&A with James Reinders on the Intel Parallel Studio Beta Program
The Pros and Cons of Outsourcing Enterprise Emails
Hosting Options: Shared or Dedicated Server

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers