[thread] multi-thread, thread attributes

来源:互联网 发布:手机维修培训软件 编辑:程序博客网 时间:2024/05/17 23:34

1. java.lang.Runnable

http://docs.oracle.com/javase/6/docs/api/java/lang/Runnable.html


public interface Runnable

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments calledrun.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example,Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassingThread. A class that implements Runnable can run without subclassingThread by instantiating a Thread instance and passing itself in as the target. In most cases, theRunnable interface should be used if you are only planning to override therun() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.


2. java.lang.thread

http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html

java.lang.Object  extended by java.lang.Thread
All Implemented Interfaces:
Runnable
public class Thread
extends Object
implements Runnable

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.

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 newThread 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 namedmain of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
  • All threads that are not daemon threads have died, either by returning from the call to therun method or by throwing an exception that propagates beyond the run method.

There are two ways to create a new thread of execution. One is to declare a class to be a subclass ofThread. This subclass should override the run method of classThread. 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 creatingThread, 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.


3. ThreadGroup

http://docs.oracle.com/javase/6/docs/api/java/lang/ThreadGroup.html

http://blog.csdn.net/liqj2ee/article/details/581220


4.

http://blog.csdn.net/dongwujing/article/details/7647858

http://zhidao.baidu.com/link?url=p0uk4RQqRejBM8yNYpqqdZ_HqzAVBh9EetYJUva8VML-VAKLT2VD-rV9U08LBezHH8laLx0G2FQKTNtaZwSelK