Pages

Monday, September 24, 2012

Running Mulitple Threads


Running Multiple Threads :

Just because a series of threads are started in a particular order doesn't mean they'll run in that order. For any group of started
threads, order is not guaranteed by the scheduler. And duration is not guaranteed. You don't know, for example, if one thread will run to completion before the others have a chance to get in or whether they'll all take turns nicely, or whether they'll do a combination of both.

There is a way, however, to start a thread but tell it not to
run until some other thread has finished. You can do this with the join() method,

Once a thread has been started, it can never be started again. If you have a reference to a Thread, and you call start(), it's started. If you call start() a second time, it will cause an exception (an IllegalThreadStateException, which is a kind of RuntimeException.

Methods influencing scheduler :


               Methods from the java.lang.Thread Class Some of the methods that can help us influence thread scheduling are as follows:

public static void sleep(long millis) throws InterruptedException
public static void yield()
public final void join() throws InterruptedException
public final void setPriority(int newPriority)


Methods from the java.lang.Object Class Every class in Java inherits the following three thread-related methods:

public final void wait() throws InterruptedException
public final void notify()
public final void notifyAll()

No comments:

Post a Comment