Thread和Object类中的重要方法
一、 方法概览
类 | 方法名 | 简介 |
---|---|---|
-- | sleep相关 | -- |
-- | join | 等待其他线程执行完毕 |
-- | yield相关 | 放弃已经获取到的CPU资源 |
Thread | currentThread | 获取当前执行线程的引用 |
-- | start/run相关 | 启动线程相关 |
-- | interrupt相关 | 中断线程 |
-- | stop/suspend/resume相关 | 已废弃 |
Object | wait/notify/notifyAll相关 | 让线程暂时休息和唤醒 |
/**
* 描述:演示打印main, Thread-0, Thread-1
*/
public class CurrentThread implements Runnable {
public static void main(String[] args) {
new CurrentThread().run();
new Thread(new CurrentThread()).start();
new Thread(new CurrentThread()).start();
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
/**
* 输出:
* main
* Thread-0
* Thread-1
*
*/
}
二、wait/notify/notifyAll
2.1 基本用法
作用/用法:阻塞阶段、唤醒阶段、遇到中断
/**
* 描述:展示wait和notify的基本用法 1. 研究代码执行顺序 2. 证明wait释放锁
*/
public class Wait {
public static Object object = new Object();
static class Thread1 extends Thread {
@Override
public void run() {
synchronized (object) {
System.out.println(Thread.currentThread().getName() + "开始执行了");
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程" + Thread.currentThread().getName() + "获取到了锁。");
}
}
}
static class Thread2 extends Thread {
@Override
public void run() {
synchronized (object) {
object.notify();
System.out.println("线程" + Thread.currentThread().getName() + "调用了notify()");
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread1 thread1 = new Thread1();
Thread2 thread2 = new Thread2();
thread1.start();
Thread.sleep(200);
thread2.start();
}
/**
* 展示wait和notify的基本用法 1. 研究代码执行顺序 2. 证明wait释放锁
*
* 输出:
* Thread-0开始执行了
* 线程Thread-1调用了notify()
* 线程Thread-0获取到了锁。
*/
}
/**
* 描述:3个线程,线程1和线程2首先被阻塞,线程3唤醒它们。notify, notifyAll。 start先执行不代表线程先启动。
*/
public class WaitNotifyAll implements Runnable {
private static final Object resourceA = new Object();
public static void main(String[] args) throws InterruptedException {
Runnable r = new WaitNotifyAll();
Thread threadA = new Thread(r);
Thread threadB = new Thread(r);
Thread threadC = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resourceA) {
resourceA.notifyAll();
// resourceA.notify();
System.out.println("ThreadC notified.");
}
}
});
threadA.start();
threadB.start();
// Thread.sleep(200);
threadC.start();
}
@Override
public void run() {
synchronized (resourceA) {
System.out.println(Thread.currentThread().getName()+" got resourceA lock.");
try {
System.out.println(Thread.currentThread().getName()+" waits to start.");
resourceA.wait();
System.out.println(Thread.currentThread().getName()+"'s waiting to end.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
*3个线程,线程1和线程2首先被阻塞,线程3唤醒它们。notify, notifyAll。 start先执行不代表线程先启动。
*
* 输出:
*
* Thread-0 got resourceA lock.
* Thread-0 waits to start.
* ThreadC notified.
* Thread-1 got resourceA lock.
* Thread-1 waits to start.
* Thread-0's waiting to end.
* ...
*
*/
}
wait只释放当前的那把锁
/**
* 描述:证明wait只释放当前的那把锁
*/
public class WaitNotifyReleaseOwnMonitor {
private static volatile Object resourceA = new Object();
private static volatile Object resourceB = new Object();
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resourceA) {
System.out.println("ThreadA got resourceA lock.");
synchronized (resourceB) {
System.out.println("ThreadA got resourceB lock.");
try {
System.out.println("ThreadA releases resourceA lock.");
resourceA.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (resourceA) {
System.out.println("ThreadB got resourceA lock.");
System.out.println("ThreadB tries to resourceB lock.");
synchronized (resourceB) {
System.out.println("ThreadB got resourceB lock.");
}
}
}
});
thread1.start();
thread2.start();
}
/**
* 证明wait只释放当前的那把锁
*
* 输出:
*
* ThreadA got resourceA lock.
* ThreadA got resourceB lock.
* ThreadA releases resourceA lock.
* ThreadB got resourceA lock.
* ThreadB tries to resourceB lock.
* ...
*
*/
}
网友评论