多线程代码演示
wait/notify演示
/**
* 演示wait()/notify()
*/
public class WaitAndNotifyTest {
public static void main(String[] args) {
Object obj= new Object();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (obj){
try {
System.out.println("等待前代码");
obj.wait(); //线程等待
System.out.println("等待后代码");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
try {
Thread.sleep(2000); //保证等待线程执行
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
synchronized (obj){
obj.notify();//唤醒等待线程
System.out.println("唤醒后代码");
}
}
}).start();
}
}
执行上面代码可以得出结论:
1.wait()/notify()必须使用同一锁对象,并且在同步代码块中调用
2.执行notify()后线程不会立即唤醒线程,当前线程执行完后才会唤醒并且释放锁对象
interrupt中断wait释放锁
/**
* 演示interrupt中断wait释放锁
*/
public class WaitAndNotifyTest {
public static void main(String[] args) {
Object obj= new Object();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (obj) {
try {
System.out.println("等待前代码");
obj.wait(); //线程等待
System.out.println("等待后代码");
} catch (InterruptedException e) {
System.out.println("报错");
}
}
}
});
thread.start();
try {
Thread.sleep(2000); //保证线程等待线程执行
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.interrupt();
}
}
一生产一消费
public class ValueOp {
private String value="";
public void setValue(String val){
synchronized (this){
if(!value.equals("")){//value有值等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
value=val;
System.out.println("set设置的值是"+value);
this.notify();
}
}
public void getValue( ){
synchronized (this){
if(value.equals("")){//value没值等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(value);
value="";
this.notify();
}
}
}
public class ProducerThread extends Thread {
ValueOp value;
public ProducerThread(ValueOp val) {
value=val;
}
@Override
public void run() {
while (true){
value.setValue(System.currentTimeMillis()+"---->"+System.nanoTime());
}
}
}
public class ConsumerThread extends Thread {
ValueOp value;
public ConsumerThread(ValueOp val) {
value=val;
}
@Override
public void run() {
while (true){
value.getValue();
}
}
}
public class ProducerConsumerTest {
public static void main(String[] args) {
ValueOp valueOp = new ValueOp();
ProducerThread producerThread = new ProducerThread(valueOp);
ConsumerThread consumerThread = new ConsumerThread(valueOp);
producerThread.start();
consumerThread.start();
}
}
多生产多消费
/**
* Created by Administrator on 2020-12-01.
*/
public class ValueOp {
private String value="";
public void setValue(String val){
synchronized (this){
while(!value.equals("")){//value有值等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
value=val;
System.out.println("set设置的值是"+value);
this.notifyAll();
}
}
public void getValue( ){
synchronized (this){
while (value.equals("")){//value没值等待,该线程如果被唤醒必须再进行判断,防止已经被其他线程消费
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("get得到的值是"+value);
value="";
this.notifyAll();
}
}
}
public class ProducerThread extends Thread {
ValueOp value;
public ProducerThread(ValueOp val) {
value=val;
}
@Override
public void run() {
while (true){
value.setValue(System.currentTimeMillis()+"---->"+System.nanoTime());
}
}
}
public class ConsumerThread extends Thread {
ValueOp value;
public ConsumerThread(ValueOp val) {
value=val;
}
@Override
public void run() {
while (true){
value.getValue();
}
}
}
public class ProducerConsumerTest {
public static void main(String[] args) {
ValueOp valueOp = new ValueOp();
ProducerThread producerThread = new ProducerThread(valueOp);
ProducerThread producerThread2 = new ProducerThread(valueOp);
ProducerThread producerThread3 = new ProducerThread(valueOp);
ConsumerThread consumerThread = new ConsumerThread(valueOp);
ConsumerThread consumerThread2 = new ConsumerThread(valueOp);
ConsumerThread consumerThread3 = new ConsumerThread(valueOp);
producerThread.start();
producerThread2.start();
producerThread3.start();
consumerThread.start();
consumerThread2.start();
consumerThread3.start();
}
}