Thread线程代码整理

thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
线程用于执行程序,Java虚拟机允许一个程序有多个线程在同一时间执行。

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:
当一个Java程序启动的时候,通常会有一个单独的非守护线程 通常也被叫做main主函数线程。Java虚拟机会继续执行其他线程除非遇到了以下几种情况:

  1. The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.在程序运行时候“exit”方法被命令执行,并且安全管理器(Security Manager)允许退出方法的执行
  2. 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.所有的非守护线程都已经死掉,无论是从调用返回到run方法或者抛出不属于run方法的exception

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.
有2种创建新线程的方式,一种是创建一个Thread类的子类的class,这个字类需要覆盖Thread的run方法,字类可以被分配地址并且被执行

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.
另一个创建线程的方式是继承Runnable接口,然后实现里面的run方法。然后这个实现了Runnable的方法可以被分配空间,可以传递参数来创建线程并启动线程。

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.
每一个线程都有被标记的标识,多个线程可能拥有相同的名字,如果一个线程已经有了特定的名称,会自动生成一个线程名。

Unless otherwise noted, passing a {@code null} argument to a constructor or method in this class will cause a {@link NullPointerException} to be thrown.
除非有特别的说明,当传递了一个null类型的参数给构造函数的时候会造成NPE空指针异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值