多线程共享互斥“栈”资源
2018-01-11 20:04
267 查看
Client.java
public class Client {
public static void main(String[] args) {
MyStack ms = new MyStack();
PushThread t1 = new PushThread(ms);
PopThread t2 = new PopThread(ms);
t1.start();
t2.start();
}
}
MyStack .java
public class MyStack {
private int[] data = new int[30];
private int idx=0;
public void push(int i) {
synchronized (this){
data[idx] = i;
System.out.println("push:"+i);
idx++;
this.notify();//唤醒在wait pool池中的某个线程
}
}
/**
* @param i
*/
public synchronized int pop() {
if(idx<=0){
try {
//CPU资源,两者都会释放的!
//Thread.sleep(10);//sleep不会释放对象锁
this.wait();//wait会释放对象锁,将线程加入wait pool池中
} catch (InterruptedException e) {
e.printStackTrace();
}
}
idx--;
System.out.println("pop:"+ data[idx]);
return data[idx];
}
}
//在段代码中,因为PushThread与PopThread线程的对象都是同一个,所以可以用同一个this对象来锁
//无论是锁方法还是同步块地锁,只要对象锁是同一个,都可以锁成---效果是一样的
PopThread .java
public class PopThread extends Thread{
private MyStack ms=null;
public PopThread(MyStack ms){
this.ms = ms;
}
@Override
public void run() {
for(int i=20;i>=0;i--){
ms.pop();
}
}
}
PushThread.java
public class PushThread extends Thread{
public MyStack ms = null;
public PushThread(MyStack ms){
this.ms = ms;
}
public void run() {
for(int i=20;i>=0; i--){
ms.push(i);
}
}
}
public class Client {
public static void main(String[] args) {
MyStack ms = new MyStack();
PushThread t1 = new PushThread(ms);
PopThread t2 = new PopThread(ms);
t1.start();
t2.start();
}
}
MyStack .java
public class MyStack {
private int[] data = new int[30];
private int idx=0;
public void push(int i) {
synchronized (this){
data[idx] = i;
System.out.println("push:"+i);
idx++;
this.notify();//唤醒在wait pool池中的某个线程
}
}
/**
* @param i
*/
public synchronized int pop() {
if(idx<=0){
try {
//CPU资源,两者都会释放的!
//Thread.sleep(10);//sleep不会释放对象锁
this.wait();//wait会释放对象锁,将线程加入wait pool池中
} catch (InterruptedException e) {
e.printStackTrace();
}
}
idx--;
System.out.println("pop:"+ data[idx]);
return data[idx];
}
}
//在段代码中,因为PushThread与PopThread线程的对象都是同一个,所以可以用同一个this对象来锁
//无论是锁方法还是同步块地锁,只要对象锁是同一个,都可以锁成---效果是一样的
PopThread .java
public class PopThread extends Thread{
private MyStack ms=null;
public PopThread(MyStack ms){
this.ms = ms;
}
@Override
public void run() {
for(int i=20;i>=0;i--){
ms.pop();
}
}
}
PushThread.java
public class PushThread extends Thread{
public MyStack ms = null;
public PushThread(MyStack ms){
this.ms = ms;
}
public void run() {
for(int i=20;i>=0; i--){
ms.push(i);
}
}
}
相关文章推荐
- 使用临界区CRITICAL_SECTION结构对象保护多线程互斥地访问共享资源
- Linux下的多进程间共享资源的互斥访问
- 多线程系列:竞态临界区、共享资源
- 多线程 Runnable 基类 —— 线程间资源的共享与独占
- java多线程互斥实战-模拟串口资源互斥
- Java学习笔记 --- 多线程共享资源Demo
- Linux下的多进程间共享资源的互斥访问
- java多线程系列----------- 共享受限资源(二)
- 实现Runnable,轻松实现多线程间的资源共享
- 黑马程序员_线程高级_多线程,同步,互斥,线程数据共享
- Java多线程设计(二)线程的基本知识(2)共享互斥
- java多线程(五)-访问共享资源以及加锁机制(synchronized,lock,voliate)
- 9、Java并发性和多线程-线程安全与共享资源
- 多线程资源共享的问题
- java多线程(二)解决共享资源竞争
- java多线程并发中使用Lockers类将多线程共享资源锁定
- 多线程共享资源参考
- 如果一个类通过继承Thread来实现多线程的话,则不适合多个线程共享资源,而通过实现Runnable就可以做到这一点
- Java多线程 之 访问共享资源synchronized、lock(七)
- 多线程共享资源冲突