Showing posts with label threads. Show all posts
Showing posts with label threads. Show all posts

Thursday, November 29, 2012

Threads in java

Problem 1:
You have thread T1, T2 and T3, how will you ensure that thread T2 start after the end of T1 and thread T3 will start after T2 end?
Solution:
By using Thread.join();
     t1.start(); 
     t1.join(); 
     t2.start(); 
     t2.join(); 
     t3.start(); 
//     t3.join();  - not needed


Problem 2:
"You have T1, T2, and T3. T1 and T2 can execute concurrently, but T3 has to wait for both of them before it can run."
Solution:
     t1.start(); 
     t2.start(); 
     t1.join(); 
     t2.join(); 
     t3.start();
 

Compare wait and sleep methods in java
Wait release the lock or monitor while sleep doesn't release any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution.

Wait is defined in Object class, where sleep methods are defined in Thread class.

Both could throw InterruptedException.


Monday, April 4, 2011

Java threads

Important methods
* constructior Thread( ThreadGroup group, Runnable target, String name ) - If the target argument is null, this thread's run method is called when this thread is started.
* static int enumerate( Thread[] tarray ) - Copies into the specified array every active thread in the current thread's thread group and its subgroups.
* int getPriority();* join() - Waits for this thread to die.
* join( long millis ) - Waits at most millis milliseconds for this thread to die.
* run() - If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
* setDaemon( boolean on ) - Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads. This method must be called before the thread is started.
* sleep(long millis) throws InterruptedException - Causes the currently executing thread to sleep (temporarily cease execution). If any thread has interrupted the current thread, InterruptedException exception is thrown.
* start() - Causes this thread to begin execution. The result is that two threads are running concurrently.
* run() - If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
* stop() - Deprecated!
* interrupt() - If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
* suspend() - Deprecated!
*

The deprecated way:
    private Thread blinker;

    public void start() {
        blinker = new Thread(this);
        blinker.start();
    }

    public void stop() {
        blinker.stop();  // UNSAFE!
    }

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (true) {
            try {
                thisThread.sleep(interval);
            } catch (InterruptedException e){
            }
            repaint();
        }
    }

The correct way:
    private volatile Thread blinker;

    public void stop() {
        blinker = null;
    }

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (blinker == thisThread) {
            try {
                thisThread.sleep(interval);
            } catch (InterruptedException e){
            }
            repaint();
        }
    } 
 
There are two ways to create a new thread of execution.
The first one is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started.
In all cases, the tread is started with run() method.