点击蓝字
关注我们
祝愿你能拿到令你心动的offer
在本文中,我们将通过实例学习重要的Thread类中的API/方法。
Thread类方法列表
该类图显示了一个java.lang.Thread类API/Methods的列表。
Thread类方法使用示例
我们来演示一下java.lang.Thread类的几个重要方法的用法。
Thread.sleep() 方法
Thread.sleep 导致当前线程在指定时间内暂停执行。这是一种有效的手段,可以让应用程序的其他线程或计算机系统上可能运行的其他应用程序获得处理器时间。
Thread.sleep()方法示例
在这个例子中,我们创建并启动了两个线程thread1和thread2。请注意,我们在这个例子中使用了sleep()方法的两个重载版本:
Thread.sleep(1000);Thread.sleep(1000, 500);/** * thread sleep method examples * @author Noseparte * */public class ThreadSleepExample { public static void main(final String[] args) { System.out.println("Thread main started"); final Thread thread1 = new Thread(new WorkerThread()); thread1.setName("WorkerThread 1"); final Thread thread2 = new Thread(new WorkerThread()); thread1.setName("WorkerThread 2"); thread1.start(); thread2.start(); System.out.println("Thread main ended"); }}class WorkerThread implements Runnable { @Override public void run() { for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); Thread.sleep(1000, 500); System.out.println("[" + Thread.currentThread().getName() + "] Message " + i); } catch (final InterruptedException e) { e.printStackTrace(); } } }}
Output:
Thread main startedThread main ended[WorkerThread 2] Message 0[Thread-1] Message 0[WorkerThread 2] Message 1[Thread-1] Message 1[WorkerThread 2] Message 2[Thread-1] Message 2[WorkerThread 2] Message 3[Thread-1] Message 3[WorkerThread 2] Message 4[Thread-1] Message 4
请注意,sleep()方法会抛出InterruptedException异常,当另一个线程在睡眠活动时中断当前线程。
Thread.join() 方法
join()方法会等待一个线程死亡。换句话说,它使当前正在运行的线程停止执行,直到与它接合的线程完成任务。
线程join()方法示例。首先,我们将创建一个任务,它将计算1-5个数字的总和(在for循环中维护)。在主线程中,让我们创建4个任务:
final Task task1 = new Task(500l);final Task task2 = new Task(1000l);final Task task3 = new Task(2000l);final Task task4 = new Task(50l);
现在,让我们创建4个线程来运行以上4个任务:
final Thread thread1 = new Thread(task1); final Thread thread2 = new Thread(task2);final Thread thread3 = new Thread(task3);final Thread thread4 = new Thread(task4);
给每个人指定名字,并开始所有的4个线程:
thread1.setName("thread-1");thread2.setName("thread-2");thread3.setName("thread-3");thread4.setName("thread-4");thread1.start();thread2.start();thread3.start();thread4.start();
在这个例子中,当目标线程完成求和时,调用者线程(main)会被唤醒,并调用task.getSum()方法,由于目标线程已经完成了工作,因此该方法肯定会包含总和。
task4的睡眠时间较小,因此它比其他线程先完成总和。因此,主线程调用了thread4.join(),但由于thread4已经完成了工作,所以立即返回其执行。
/** * This class demonstrate the how join method works with an example. * @author Noseparte * */public class ThreadJoinExample { public static void main(final String[] args) throws InterruptedException { System.out.println("Thread main started"); final Task task1 = new Task(500l); final Task task2 = new Task(1000l); final Task task3 = new Task(2000l); final Task task4 = new Task(50l); final Thread thread1 = new Thread(task1); final Thread thread2 = new Thread(task2); final Thread thread3 = new Thread(task3); final Thread thread4 = new Thread(task4); thread1.setName("thread-1"); thread2.setName("thread-2"); thread3.setName("thread-3"); thread4.setName("thread-4"); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.out.println("[" + Thread.currentThread().getName() + "] waiting for " + thread1.getName()); thread1.join(); System.out.println(thread1.getName() + " finished! Result: " + task1.getSum()); System.out.println("[" + Thread.currentThread().getName() + "] waiting for " + thread2.getName()); thread2.join(); System.out.println(thread2.getName() + " finished! Result: " + task2.getSum()); System.out.println("[" + Thread.currentThread().getName() + "] waiting for " + thread3.getName()); thread3.join(); System.out.println(thread3.getName() + " finished! Result: " + task3.getSum()); // As thread-4 already finished (smaller sleep time), the join call only immediately // returns the control to the caller thread System.out.println("[" + Thread.currentThread().getName() + "] waiting for " + thread4.getName()); thread4.join(); System.out.println(thread4.getName() + " finished! Result: " + task4.getSum()); System.out.println("Thread main finished"); }}class Task implements Runnable { private long sleep; private int sum; public Task(final long sleep) { this.sleep = sleep; } @Override public void run() { for (int i = 1; i <= 5; i++) { System.out.println("[" + Thread.currentThread().getName() + "] Adding " + i); sum += i; try { Thread.sleep(sleep); } catch (final InterruptedException e) { e.printStackTrace(); } } } public int getSum() { return this.sum; }}
Output:
Thread main started[thread-1] Adding 1[thread-2] Adding 1[thread-3] Adding 1[main] waiting for thread-1[thread-4] Adding 1[thread-4] Adding 2[thread-4] Adding 3[thread-4] Adding 4[thread-4] Adding 5[thread-1] Adding 2[thread-1] Adding 3[thread-2] Adding 2[thread-1] Adding 4[thread-1] Adding 5[thread-3] Adding 2[thread-2] Adding 3thread-1 finished! Result: 15[main] waiting for thread-2[thread-2] Adding 4[thread-2] Adding 5[thread-3] Adding 3thread-2 finished! Result: 15[main] waiting for thread-3[thread-3] Adding 4[thread-3] Adding 5thread-3 finished! Result: 15[main] waiting for thread-4thread-4 finished! Result: 15Thread main finished
注意,输出,main()线程最后完成了它的执行。试着通过查看一个输出来理解这个例子。
join()方法会抛出一个InterruptedException--如果有线程中断了当前线程。当这个异常被抛出时,当前线程的中断状态被清除。
Thread.interrupt() 方法
中断是对线程的一种指示,表明它应该停止正在做的事情,做别的事情。线程对中断的具体响应方式由程序员决定,但线程终止的情况非常普遍。
java.lang Thread类提供了三个interrupt()方法来正常工作Interrupts。
void interrupt()--中断这个线程。
static boolean interrupted()--测试当前线程是否被中断。
3.boolean isInterrupted() - 测试该线程是否被中断。
Thread.interrupt()方法示例:
public class TerminateTaskUsingThreadAPI { public static void main(final String[] args) {System.out.println("Thread main started"); final Task task = new Task(); final Thread thread = new Thread(task); thread.start(); thread.interrupt(); System.out.println("Thread main finished"); }}class Task implements Runnable { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println("[" + Thread.currentThread().getName() + "] Message " + i); if (Thread.interrupted()) { System.out.println("This thread was interruped by someone calling this Thread.interrupt()"); System.out.println("Cancelling task running in thread " + Thread.currentThread().getName()); System.out.println("After Thread.interrupted() call, JVM reset the interrupted value to: " + Thread.interrupted()); break; } } }}
Output:
Thread main startedThread main finished[Thread-0] Message 0This thread was interruped by someone calling this Thread.interrupt()Cancelling task running in thread Thread-0After Thread.interrupted() call, JVM reset the interrupted value to: false
注意这里的任务是被终止的,而不是线程。
Thread.isAlive() 方法
java.lang.Thread类提供了isAlive()方法来测试这个线程是否活着。如果一个线程已经启动并且还没有死亡,那么这个线程就是活着的。
线程isAlive方法示例:
创建俩个线程:
final Thread thread1 = new Thread(new MyTask());final Thread thread2 = new Thread(new MyTask());
在用start()方法启动线程之前,只需打印检查线程是否活着。
System.out.println("Thread1 is alive? " + thread1.isAlive());System.out.println("Thread2 is alive? " + thread2.isAlive());
启动线程,再检查一下线程是否存活。
thread1.start();thread2.start();while (thread1.isAlive() || thread2.isAlive()) { System.out.println("Thread1 is alive? " + thread1.isAlive()); System.out.println("Thread2 is alive? " + thread2.isAlive()); Thread.sleep(500l);}
让我们把所有完整的程序放在一起。:
public class CheckIfThreadIsAliveUsingThreadAPI { public static void main(final String[] args) throws InterruptedException { System.out.println("Thread main started"); final Thread thread1 = new Thread(new MyTask()); final Thread thread2 = new Thread(new MyTask()); System.out.println("Thread1 is alive? " + thread1.isAlive()); System.out.println("Thread2 is alive? " + thread2.isAlive()); thread1.start(); thread2.start(); while (thread1.isAlive() || thread2.isAlive()) { System.out.println("Thread1 is alive? " + thread1.isAlive()); System.out.println("Thread2 is alive? " + thread2.isAlive()); Thread.sleep(500l); } System.out.println("Thread1 is alive? " + thread1.isAlive()); System.out.println("Thread2 is alive? " + thread2.isAlive()); System.out.println("Thread main finished"); }}class MyTask implements Runnable { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println("[" + Thread.currentThread().getName() + "] Message " + i); try { Thread.sleep(200); } catch (final InterruptedException e) { e.printStackTrace(); } } }}
Output:
Thread main startedThread1 is alive? falseThread2 is alive? falseThread1 is alive? trueThread2 is alive? true[Thread-0] Message 0[Thread-1] Message 0[Thread-1] Message 1[Thread-0] Message 1[Thread-1] Message 2[Thread-0] Message 2Thread1 is alive? trueThread2 is alive? true[Thread-0] Message 3[Thread-1] Message 3[Thread-1] Message 4[Thread-0] Message 4Thread1 is alive? falseThread2 is alive? falseThread main finished
Thread.setPriority() 方法
每个线程都有一个优先级。优先级用1到10之间的数字表示。在大多数情况下,线程调度器根据线程的优先级来调度线程(称为抢先调度)。但这并不能保证,因为它选择哪种调度方式取决于JVM规范。
Thread类中定义了三个常量。
public static int MIN_PRIORITY。
public static int NORM_PRIORITY 静态的NORM_PRIORITY。
public static int MAX_PRIORITY 线程的默认优先级为5(NORM_PRIORITY)。MIN_PRIORITY的值是1,MAX_PRIORITY的值是10。
为线程设置优先级 示例
public class ThreadPriorityExample { public static void main(final String[] args) { final Runnable runnable = () -> { System.out.println("Running thread name : " + Thread.currentThread().getName() + " and it's priority : " + Thread.currentThread().getPriority()); }; final Thread thread1 = new Thread(runnable); final Thread thread2 = new Thread(runnable); final Thread thread3 = new Thread(runnable); final Thread thread4 = new Thread(runnable); thread1.setPriority(Thread.MIN_PRIORITY); thread2.setPriority(Thread.NORM_PRIORITY); thread3.setPriority(Thread.MAX_PRIORITY); thread4.setPriority(2); thread1.start(); thread2.start(); thread3.start(); thread4.start(); }}
Output:
Running thread name : Thread-0 and it's priority : 1Running thread name : Thread-1 and it's priority : 5Running thread name : Thread-2 and it's priority : 10Running thread name : Thread-3 and it's priority : 2
Thread.setName() 方法
java.lang.Thread类提供了改变和获取线程名称的方法。默认情况下,每个线程都有一个名字,即thread-0、thread-1等。我们可以通过使用setName()方法来改变线程的名称。下面给出了setName()和getName()方法的语法。
public String getName():用于返回线程的名称。
公共void setName(String name) 用于更改线程的名称。
Thread类提供了一个静态的currentThread()--返回一个对当前执行的线程对象的引用。
命名一个线程实例:
/** * Thread naming example using Thread class. * @author Noseparte **/public class ThreadExample { public static void main(final String[] args) { System.out.println("Thread main started"); final Thread thread1 = new WorkerThread(); thread1.setName("WorkerThread1"); final Thread thread2 = new WorkerThread(); thread2.setName("WorkerThread2"); final Thread thread3 = new WorkerThread(); thread3.setName("WorkerThread3"); final Thread thread4 = new WorkerThread(); thread4.setName("WorkerThread4"); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.out.println("Thread main finished"); }}class WorkerThread extends Thread { @Override public void run() { System.out.println("Thread Name :: " + Thread.currentThread().getName()); }}
Output:
Thread main startedThread Name :: WorkerThread1Thread Name :: WorkerThread2Thread Name :: WorkerThread3Thread main finishedThread Name :: WorkerThread4
、
球分享
球点赞
![cc45a852c18366e7159fa059667d1b84.gif](https://img-blog.csdnimg.cn/img_convert/cc45a852c18366e7159fa059667d1b84.gif)
球在看