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.

No comments:

Post a Comment