常用方法
构造方法
- Thread()
- Thread(Runnable target) //分配新的 Thread 对象。
- Thread(Runnable target, String name)
- Thread(String name)
- Thread(ThreadGroup group, Runnable target) //线程组的一员。
- Thread(ThreadGroup group, Runnable target, String name) //将 target 作为其运行对象
- Thread(ThreadGroup group, Runnable target, String name, long stackSize) //具有指定的堆栈大小。
- Thread(ThreadGroup group, String name)
获取信息
- static int activeCount() //返回当前线程的线程组中活动线程的数目
- long getId() //返回该线程的标识符。
- String getName() //返回该线程的名称。
- int getPriority() //返回线程的优先级 。
- Thread.State getState() //返回该线程的状态。
获取对象
- static Thread currentThread() //返回对当前正在执行的线程对象的引用
- static int enumerate(Thread[] tarray) //将当前线程的线程组及其子组中的每一个活动线程复制到指定的数组中
- ClassLoader getContextClassLoader() //返回该线程的上下文 ClassLoader
- ThreadGroup getThreadGroup() //返回该线程所属的线程组
获取状态
- static boolean interrupted() //测试当前线程是否已经中断,将状态标志置清除为false。
- boolean isInterrupted() //测试线程Thread对象是否已经中断,不清除状态标志。
- boolean isAlive() //测试线程是否处于活动状态。
- boolean isDaemon() //测试该线程是否为守护线程。
- static boolean holdsLock(Object obj) //线程在指定的对象上保持监视器锁时,才返回 true
修改信息
- void setContextClassLoader(ClassLoader cl) //设置该线程的上下文 ClassLoader。
- void setDaemon(boolean on) //将该线程标记为守护线程或用户线程
- void setPriority(int newPriority) //更改线程的优先级
修改状态
- void interrupt() //中断线程。
- void join() //等待该线程终止。
- void join(long millis) //等待该线程终止的时间最长为 millis 毫秒。
- void join(long millis, int nanos) //等待该线程终止的时间最长为 millis 毫秒 + nanos 纳秒
- void run() //如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。
- static void sleep(long millis) //让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。
- static void sleep(long millis, int nanos)
- void start() //使该线程开始执行;Java 虚拟机调用该线程的 run 方法
- static void yield() //暂停当前正在执行的线程对象,并执行其他线程。放弃当前cpu资源,给线程资源。放弃时间不定。
获取线程信息
实例:main函数的对应main线程
public class Test {
public static void main(String[] args){
System.out.println(Thread.currentThread());//Thread[main,5,main]
System.out.println(Thread.currentThread().getId());//1
System.out.println(Thread.currentThread().getName());//main
System.out.println(Thread.currentThread().getState());//RUNNABLE
System.out.println(Thread.currentThread().getPriority());//5
}
}
创建线程
- 继承Thread类
- 实现Runnable接口
实例 : 计算大于某一规定值的质数的线程
//(继承Thread类)
class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
}
//重写run方法
public void run() {
// compute primes larger than minPrime
. . .
}
}
//(实现Runnable接口)
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
启动线程
//继承Thread类的线程类
public class Test {
public static void main(String[] args) {
Thread A= new PrimeRun(100);
A.start();//启动线程
}
}
//实现Runnable接口的线程类
public class Test {
public static void main(String[] args) {
Thread A= new Thread(new PrimeRun(100));
A.start();//启动线程
}
}
休眠线程
实例:设置倒计时
//测试类
public class Test {
public static void main(String[] args) {
Thread A= new Thread(new Thread_A(),"A");
A.start();
}
}
//线程类
public class Thread_A implements Runnable {
@Override
public void run() {
int i=10;
while (i>0){
System.out.println("剩余时间: "+i+" 秒");
try {
Thread.sleep(1000);//休眠1000ms 即1秒
} catch (InterruptedException e) {
e.printStackTrace();
}
i--;
}
}
}
停止线程
- 使用退出标志,使线程正常退出,也就是run方法完成后线程终止
- 使用stop()方法强制退出.(已作废)
- 使用interrupt()方法中断线程(只是打上终止标志,并不是立刻停止线程)
- 异常法(建议使用,会抛出异常,方便处理)
- return法
//异常法终止线程
public class Thread_B extends Thread {
@Override
public void run() {
super.run();
try {
for (int i = 0; i < 1000000; i++) {
if (this.isInterrupted()) {
System.out.println("已经是停止状态!");
throw new InterruptedException();
}
System.out.println("i =" + (i + 1)); //i =74842
}
System.out.println("如果输出,表示,线程终止后,仍然会继续运行,线程未停止");//非异常法会继续运行。
} catch (InterruptedException e) {
System.out.println("run catch");
e.printStackTrace();
}
}
}
//运行类
public class Run {
public static void main(String[] args){
Thread B = new Thread_B();
B.start();
try {
Thread.sleep(1000);
B.interrupt();
} catch (Exception e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("ending");
}
}
//return法
public class Thread_C extends Thread {
long start = System.currentTimeMillis();
@Override
public void run() {
super.run();
while (true)
{
if (this.isInterrupted()){
System.out.println("线程已停止!");
System.out.println(System.currentTimeMillis()-start);
return;
}
System.out.println(System.currentTimeMillis());
}
}
}
public class Run {
public static void main(String[] args){
Thread C =new Thread_C();
C.start();
try {
Thread.sleep(2000);
C.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
注意事项
- stop() //停止线程,不建议使用可能导致:1,清理性工作不能完成,2解锁后,数据不能同步
- suspend(),resume()//独占资源,容易造成死锁,不同步。