Thursday, June 18, 2009

Multi-threading in Java

Multi threading is really a nice feature a programming language can have. In multi threading you can make new threads in your program while it is running, which means program will be running in more than one thread simultaneously(parallel). It can be a very useful feature in some server client system. In java you can use multi threading in the following way.

points
1) to be able to run a class in a thread the class need to either extend Thread class or should implement Runnable.

2) The second requirement to be able to run a class into a thread is to have a method named run(), this method will contain what to run in the thread.


public ClassName extends Thread{
ClassName(){
}
run(){
}
}

public ClassName implements Runnable{
ClassName(){
}
run(){
}
}

3) Syntax to start the thread in some other class

ClassName name = new ClassName();
Thread t = newThread(name);
t.start();

No comments:

Post a Comment