Classe java.lang.Thread


A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
	package java.lang;

		(>JDK1.0)public
	class Thread implements Runnable {

	    // Constantes
		(>JDK1.0)    public final static int MAX_PRIORITY = 10;
		(>JDK1.0)    public final static int MIN_PRIORITY = 1;
		(>JDK1.0)    public final static int NORM_PRIORITY = 5;

	    // Constructeurs publiques
		(>JDK1.0)    public Thread();
		(>JDK1.0)    public Thread(Runnable target);
		(>JDK1.0)    public Thread(Runnable target, String name);
		(>JDK1.0)    public Thread(String name);
		(>JDK1.0)    public Thread(ThreadGroup group, Runnable target);
		(>JDK1.0)    public Thread(ThreadGroup group, Runnable target, String name);
		(>JDK1.0)    public Thread(ThreadGroup group, String name);

	    // Méthodes de classe publiques
		(>JDK1.0)    public static int activeCount();
		(>JDK1.0)    public static native Thread currentThread();
		(>JDK1.0)    public static void dumpStack();
		(>JDK1.0)    public static int enumerate(Thread tarray[]);
		(>JDK1.0)    public static boolean interrupted();
		(>JDK1.0)    public static native void sleep(long millis)
		throws InterruptedException;
		(>JDK1.0)    public static void sleep(long millis, int nanos) 
		throws InterruptedException;
		(>JDK1.0)    public static native void yield();

	    // Méthodes d'instance publiques
		(>JDK1.0)    public void checkAccess();
		(>JDK1.0)    public native int countStackFrames();
		(>JDK1.0)    public void destroy();
		(>JDK1.0)    public final String getName();
		(>JDK1.0)    public final int getPriority();
		(>JDK1.0)    public final ThreadGroup getThreadGroup();
		(>JDK1.0)    public void interrupt();
		(>JDK1.0)    public final native boolean isAlive();
		(>JDK1.0)    public final boolean isDaemon();
		(>JDK1.0)    public boolean isInterrupted();
		(>JDK1.0)    public final void join()
		throws InterruptedException;
		(>JDK1.0)    public final synchronized void join(long millis)
		throws InterruptedException;
		(>JDK1.0)    public final synchronized void join(long millis, int nanos)
		throws InterruptedException;
		(>JDK1.0)    public final void resume();
		(>JDK1.0)    public void run();
		(>JDK1.0)    public final void setDaemon(boolean on);
		(>JDK1.0)    public final void setName(String name);
		(>JDK1.0)    public final void setPriority(int newPriority);
		(>JDK1.0)    public synchronized native void start();
		(>JDK1.0)    public final void stop();
		(>JDK1.0)    public final synchronized void stop(Throwable o);
		(>JDK1.0)    public final void suspend();
		(>JDK1.0)    public String toString();
	}

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows:


    class PrimeThread extends Thread {
        long minPrime;
        PrimeThread(long minPrime) {
            this.minPrime = minPrime;
        }

        public void run() {
            // compute primes larger than minPrime
             . . .
        }
    }

The following code would then create a thread and start it running:

    PrimeThread p = new PrimeThread(143);
    p.start();

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. The same example in this other style looks like the following:


    class PrimeRun implements Runnable {
        long minPrime;
        PrimeRun(long minPrime) {
            this.minPrime = minPrime;
        }

        public void run() {
            // compute primes larger than minPrime
             . . .
        }
    }

The following code would then create a thread and start it running:

    PrimeRun p = new PrimeRun(143);
    new Thread(p).start();

Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.


Christophe Merlet
redfox@redfoxcenter.org
©Tous droits réservés
11 septembre 1998