Multithreading, sing and dance simultaneously.

Multithreading! Yeah, you can sing and dance at the same time.

Java gives a very strong feature of Multithreading, where a program can be broken into pieces and run concurrently.
A Java process can be divided into a number of threads and each thread is responsibile for executing a block of statements or expression. If one thread stops its execution, the execution control can be shifted to another thread. Remember, here all threads belong to the same process. The ultimate result is microprocessor idle time is reduced. The end result is output of the program comes earlier.

Thread thread = new Thread(); // Creates the Thread Object
thread.start(); // Invokes start() and executes the statements in run().

public void run(){
//Code that should be executed by thread
}

Yeah, that’s it. And you are good to go. Just now you have created a thread.
What Multithreading can do for you:
– You can code in your IDE while playing a media player.
– Transferring data to the printer while typing some file.
– Download the media and browsing the web.

API for Multithreading in Java:

1. Thread class : sleep(), join(), suspend(), resume(), stop() and run() etc.
2. Runnable interface : only run() method
3. Object : wait(), notify() and notifyAll()
4. ThreadGroup : getName(), getParent() and activeCount() etc.

Threads can be created in two ways in Java:

1. Extending a Thread class.

public class Test extends Thread
{
public void run()
{
try
{
for( int i = 0; i < 10; i++)
{
System.out.println("Welcome to threads: " + i );
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.err.println("Threading is disturpted "+e);
}
System.out.println("Successful");
}
public static void main(String args[])
{
Test d1 = new Test();
d1.start();
}
}

2. Implementing runnable interface.

public class Test implements Runnable{
public void run() {
for(int i=0; i<=5;i++){
System.out.println("Executing thread "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.err.println("Threading is disturpted "+e);
}
}
System.out.println("Successful");
}
public static void main(String[] args) {
Runnable myRunnable = new Test();
Thread thread = new Thread(myRunnable);
thread.run();
}
}

In the above program we have implemented the Runnable Interface which had run method.

The thread is created, occupies some memory, but still it is inactive. An inactive thread is not eligible for microprocessor time even if the processor is idle. To make the thread active, call start() method on the thread. Now, thread becomes active and eligible for a time-slice. start() is a method of Thread class. Forgetting to call the start() method makes the thread to die in inactive state itself without performing the job assigned.

Note that:

public static native void sleep(long milliseconds) throws
InterruptedException

It is a static method throwing InterruptedException. Being a checked exception, whenever the sleep() method is used, the InterruptedException must be handled, else the code does not compile.

More to come, Stay tuned !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s