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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs