Advertisment

Multithreading in Java

author-image
PCQ Bureau
New Update

Multi-threading is all about breaking large applications into several smaller pieces to boost performance. A thread is start and end at clearly identifiable points. They may run either cooperatively or preemptively. In the cooperative model, the software understands the needs of other software running in the same system and gives up control to the OS to let other software run. When threads either do not have cooperative capability or the OS assumes they do not posses that capability and control is forcibly snatched at periodic intervals to let other applications have their chance at the CPU - this is the pre-emptive model.

Advertisment

Thread lifecycles



When a UI form is loaded and displayed, it goes through the Load, Resize, Activated and Focus events. In the same way, when a thread is instantiated, it immediately goes into the newly created state. 

Direct Hit!
Applies to:

Java programmers
USP:

Write a simple Java application that runs multiple threads and run it in Eclipse IDE
Links:

www.java.sun.com 
On PCQ Essential CD: system\labs\multithreading in java

Threads can be in the newly created, runnable, blocking or dead states. Runnable threads can also be in the queued state. Newly created threads have just come into existence and runnable ones are currently executing. When a thread is queued, it is waiting to be run. Threads that are blocking are waiting for something to happen, like user input or a disk read/write. Threads that are 'dead' have ceased running and will no longer get resource allocations. 

Advertisment

Threading in Java



To implement threading in your applications, Java includes a a pre-built thread class that you can extend or you can build your own class and implement the Runnable interface. State changes in Java threads are controlled by the JVM or JRE's scheduler. Threads can switch from runnable to queued by calling the yield() method. Similarly, it may choose to block by calling sleep(), suspend() or wait() methods. Threads die by calling stop(), from any state.

By example



On the PCQ Decemeber 2004 CD, we had given the Eclipse IDE. We shall use this with the code (bank.java) we are providing on this month's Essential CD to demonstrate a simple threading example.We assume Eclipse is already installed on your system. 

Bank.java is a typical banking transaction. There are two customers, with one thread each.A customer has an initial bank balance and this is updated with a new balance. The file has three classes, bank, bankthread and balance. The Bank class starts the program execution and also two threads. Bankthread extends the Thread class to execute the Run method and remains on a separate thread. The Balance class does balance updation calculations (getAmount() and setAmount() methods).



Let's launch Eclipse and select a location for our workspace. Select Window>Open Perspective>Java workbench menu. Next, select File>Project>Java>Java project. Click Next and give the project a name, say 'Java'. Click Next again and select the 'Allow output folders for source folders' option. Click Finish. 

To create the classes, right click on the project name ('Java' in our case) from the Package Explorer and select New>Class. Enter 'bank' for the class name and click Finish. A text window with the title 'bank.java' will be displayed with some default code. Delete all this code and copy and paste the code from our bank.java file. You can simply run the project now by clicking on Run>Run As>'Java Application'. The IDE's output window will show you the thread's output.

Sushil Oswal

Advertisment