Java Thread

What are java Threads?
How to use runnable interface in Threads?
How to run two tasks simultaneously using Threads?


Threads
are essentially subprocesses.you can think of them as tasks that belong to a program and that can run "simultaneously". Depending on the number of CPUs available and the number of competing threads, some of those threads actually will run in parallel on different CPUs, whilst in other cases the illusion of simultaneous execution will be achieved by "juggling" threads in and out of the available CPUs.For good understanding on thread we are going to write a program that splits itself into two simultaneous tasks :One task is to print Hello every second. The other task is to print Goodbye every two seconds.For them to run simultaneously, each of these two tasks will run in a separate thread. To define a "task", we create an instance of Runnable. Then we will wrap each of these Runnables around a Thread object.


Runnable
A Runnable object defines an actual task that is to be executed. It doesn't define how it is to be executed. We can define runnable as follows:

Runnable r = new Runnable(){
public void run(){
... code to be executed ...
}
};


Runnable is actually an interface, with the single run() method that we must provide. In our case, we want the Runnable.run() methods of our two tasks to print a message periodically. So here is what the code could look like:

Runnable r1 = new Runnable() {
public void run() {
try {
while (true) {
System.out.println("Hello,");
Thread.sleep(1000L);
}
} catch (InterruptedException iex) {} }
};
Runnable r2 = new Runnable() {
public void run() {
try {
while (true) {
System.out.println("Goodbye ");
Thread.sleep(2000L);
}
} catch (InterruptedException iex) {}
}
};


As you've probably gathered, the Thread.sleep() method essentially "pauses" for the given number of milliseconds, but could get "interrupted", hence the need to catch InterruptedException.The important point for now is that with the Runnable() interface, we're just defining what the two tasks are. We haven't actually set them running yet. And that's where the Thread class comes in...Thread .A Java Thread object wraps around an actual thread of execution. It effectively defines how the task is to be executed— namely, at the same time as other threads. To run the above two tasks simultaneously, we create a Thread object for each Runnable, then call the start() method on each Thread:


Thread thr1 = new Thread(r1);
Thread thr2 = new Thread(r2);
thr1.start();
thr2.start();

When we call start(), a new thread is spawned, which will begin executing the task that was assigned to the Thread object at some time in the near future. Meanwhile, control returns to the caller of start(), and we can start the second thread. Once that starts, we'll actually have at least three threads now running in parallel: the two we've just started, plus the "main" thread from which we created and started the two others.


Post a Comment

Previous Post Next Post