Java API:Thread

本文详细介绍了Java线程的基础概念,包括线程的创建方式、生命周期及管理等,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一次翻译,从APi开始。。。

API地址


java.lang
Class Thread

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 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:

  • 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 the run method or by throwing an exception that propagates beyond the run method.
线程是一个程序的执行过程。Java虚拟机让一个应用拥有多个线程且可以同时执行。

线程有优先级。优先级高的线程先于优先级低的线程执行。某个线程可能会被标识为守护进程。当在线程里创建一个新的线程对象,新线程的优先级最初设定为创建线程的优先级,当且仅当创建线程是守护进程,则新线程也是守护进程。

当Java虚拟机启动,通常会启动一个非守护进程(在一些类中它是main方法).Java虚拟机继续执行线程直到以下两种情况发生:

1 Runtime类中的exit方法被调用,并且安全管理允许这样的操作。

2 所有非守护进程的线程都停止,可能是run方法的返回,也可能是抛出异常传播到run方法。




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
              . . .
         }
     }
 
有两种方法来创建新线程。第一种方法是声明一个类为Thread类的子类,这个子类必须重载Thread类的run方法( 这里难道不是重写么。。),这之后子类的实例才能分配到内存并开始执行。举个栗子,一个线程计算大于规定值的素数可以这样写。。。

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
              . . .
         }
     }
 
创建线程的第二种方法是声明一个类实现Runnable接口。这个类也会实现run方法。类的实例能被分配,也能在线程创建时被当作参数传递。一个栗子是这样滴。。。

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.

每个线程都有标识目的的名字。多个线程可能有相同的名字。当线程创建时没有为其指定一个名字,那么将生成一个新名字。


Constructor Summary
Thread()
          Allocates a new Thread object.
Thread(Runnable target)
          Allocates a new Thread object.
Thread(Runnable target, String name)
          Allocates a new Thread object.
Thread(String name)
          Allocates a new Thread object.
Thread(ThreadGroup group, Runnable target)
          Allocates a new Thread object.
Thread(ThreadGroup group, Runnable target, String name)
          Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.
Thread(ThreadGroup group, Runnable target, String name, long stackSize)
          Allocates a new Thread object so that it has target as its run object, has the specified name as its name, belongs to the thread group referred to by group, and has the specified stack size.
Thread(ThreadGroup group, String name)
          Allocates a new Thread object.


Method Summary
static intactiveCount()
          返回当前线程所在线程组的活跃线程数目。
 voidcheckAccess()
          确定当前运行线程是否运行修改。
protected  Objectclone()
          如果当前对象的类是可复制的,返回一个对象的克隆。
 intcountStackFrames()
          Deprecated. 
static ThreadcurrentThread()
          返回当前正在执行的线程对象的引用。
 voiddestroy()
          Deprecated. 
static voiddumpStack()
         打印当前线程到标准错误流的堆栈。
static intenumerate(Thread[] tarray)
         把当前线程组和子组中的所有活跃线程复制到指定数组。
static Map<Thread,StackTraceElement[]>getAllStackTraces()
          返回所有活动线程的堆栈。
 ClassLoadergetContextClassLoader()
          返回该进程的上下文类加载器。
static Thread.UncaughtExceptionHandlergetDefaultUncaughtExceptionHandler()
          返回线程因为异常终止时的默认处理Handler。
 longgetId()
          返回线程的标识号。
 StringgetName()
          返回线程的名字。
 intgetPriority()
          返回线程的优先级。
 StackTraceElement[]getStackTrace()
          。。。
 Thread.StategetState()
          返回线程状态
 ThreadGroupgetThreadGroup()
          返回线程所在的线程组。
 Thread.UncaughtExceptionHandlergetUncaughtExceptionHandler()
          返回线程因为异常终止时的处理Handler,非static。
static booleanholdsLock(Object obj)
         如果当前线程拥有指定对象上的监视锁时返回true。
 voidinterrupt()
          中断线程。
static booleaninterrupted()
         测试当前线程有没有被中断。
 booleanisAlive()
          测试当前线程是否活跃。
 booleanisDaemon()
          测试当前线程是否是守护线程
 booleanisInterrupted()
          测试当前线程有没有被中断,非static。
 voidjoin()
          等待线程结束后再执行join之后的代码。
 voidjoin(long millis)
          。。。
 voidjoin(long millis, int nanos)
          。。。
 voidresume()
          Deprecated. 
 voidrun()
         。。。
 voidsetContextClassLoader(ClassLoader cl)
          设置该线程的上下文类加载器。
 voidsetDaemon(boolean on)
          标识该线程是一个守护线程还是用户线程。
static voidsetDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
         当线程因为异常中止时为线程设置默认处理handler。
 voidsetName(String name)
          设置线程名称。
 voidsetPriority(int newPriority)
          设置线程优先级。
 voidsetUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
          当线程因为异常中止时为线程设置默认处理handler,非static。
static voidsleep(long millis)
          使当前进程停止运行的毫秒数。
static voidsleep(long millis, int nanos)
         。。。
 voidstart()
         启动线程,JVM会调用run方法。
 voidstop()
          Deprecated.
 voidstop(Throwable obj)
          Deprecated. 
 voidsuspend()
          Deprecated. 
 StringtoString()
          返回线程的字符串表现形式,包括线程的名称、优先级和线程组。
static voidyield()
          使当前线程暂时停顿并允许其他线程执行。


方法真多,终于弄完了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值