Java Development
Articles
Java Threads
Published: September 4, 2007
Print    Email

The Java platform has supported multithreaded programming with the Thread class and Runnable interface since Java 1.0.

A thread is a unit of program execution that runs independently from other threads.Java makes it easy to define and work with multiple threads of execution within a program.

One of the commonly used classes in java Thread program is the java.lang.Thread which represents a thread and defines methods for setting and querying thread properties and for starting the execution of a thread.

Two ways to define a thread.

  • One is to subclass Thread, override the run( ) method and then instantiate your Thread subclass.
  • The other is to define a class that implements the Runnable method and then pass an instance of this Runnable object to the Thread() constructor.

An example by Implementing the Runnable Interface

/**
 * Created by IntelliJ IDEA.
 * User: xu guanglin
 * Date: 2007-9-11
 * Time: 22:11:28
 * Copyright 2005-2007 Java Development.
 * All Rights Reserved.
 * ===================================================
 * http://www.javadevelopment.org/
 * ===================================================
 * The above paragraph must be included in full.
 */
class Counter implements Runnable {
    private int currentValue;
    private Thread thread;

    public Counter(String threadName) {
        currentValue = 0;
        thread = new Thread(this, threadName);   // Create a new thread.
        System.out.println(thread);
        thread.start();                          // Start the thread.
    }

    public int getValue() {
        return currentValue;
    }

    public void run() {                          //  Thread entry point
        try {
            while (currentValue < 5) {
                System.out.println(thread.getName() + ": " + (currentValue++));
                Thread.sleep(250);               //   Current thread sleeps.
            }
        } catch (InterruptedException e) {
            System.out.println(thread.getName() + " interrupted.");
        }
        System.out.println("Exit from thread: " + thread.getName());
    }
}

 

 /**
 * Created by IntelliJ IDEA.
 * User: xu guanglin
 * Date: 2007-9-11
 * Time: 22:18:23
 * Copyright 2005-2007 Java Development.
 * All Rights Reserved.
 * ===================================================
 * http://www.javadevelopment.org/
 * ===================================================
 * The above paragraph must be included in full.
 */
public class ThreadDemo {
    public static void main(String[] args) {
        Counter counterA = new Counter("Counter A"); //  Create a thread.

        try {
            int val;
            do {
                val = counterA.getValue();       // Access the counter value.
                System.out.println("Counter value read by main thread: " + val);
                Thread.sleep(1000);              //  Current thread sleeps.
            } while (val < 5);
        } catch (InterruptedException e) {
            System.out.println("main thread interrupted.");
        }

        System.out.println("Exit from main() method.");
    }
}



View Comments (0)
Latest Articles
Java Threads

Delete file using File.delete( ) method

Copying Files in Java

Downloading the Contents of a URL

Java FTP Source Code

Processing XML with Java

Generating Random Numbers

Java Lexical Structure

Serializing

Learning java
Latest Posts
Last news from Bahrain islands
Posted by ribrothetit

Please, help me
Posted by FthrOfIIIChlds

Beginners tutorial!
Posted by perz

Health and Beauty FOREVER
Posted by SKACTANDATE

buy xanax fergana
Posted by FleseeKip

Newsletter
Subscribe to our Newsletter!

Your Email: