Java Can';不能使临界截面原子化
我目前正在学习关键部分和信号量,我被这部分困住了。我希望你们能给我一些启示 我有三种类型的线程:一种在堆栈上执行pop(),另一种在同一堆栈上执行push(),最后一种打印该堆栈的值。目前,我已经将wait()和signal()放在我假设的关键部分上Java Can';不能使临界截面原子化,java,multithreading,mutex,critical-section,Java,Multithreading,Mutex,Critical Section,我目前正在学习关键部分和信号量,我被这部分困住了。我希望你们能给我一些启示 我有三种类型的线程:一种在堆栈上执行pop(),另一种在同一堆栈上执行push(),最后一种打印该堆栈的值。目前,我已经将wait()和signal()放在我假设的关键部分上 class Example{ public static void main(){ //create thread objects StackPop p1 = new StackPop(); StackPop p2
class Example{
public static void main(){
//create thread objects
StackPop p1 = new StackPop();
StackPop p2 = new StackPop();
StackPop p3 = new StackPop();
StackPush ps1 = new StackPush();
StackPush ps2 = new StackPush();
StackPush ps3 = new StackPush();
StackValues s1 = new StackValues();
StackValues s2 = new StackValues();
StackValues s3 = new StackValues();
//then we start these threads in mix orders
p1.start();
s3.start();
ps2.start();
// etc
}
}
class StackPop extends Thread{
public void run(){
mutex.wait();
pop();
SOP("value popped is " + get.popVal);
mutex.signal();
}
}
class StackPush extends Thread{
public void run(){
mutex.wait();
push();
SOP("value inserted is " + get.pushVal);
mutex.signal();
}
}
class StackValues extends Thread{
public void run(){
mutex.wait();
for(int i = 0; i<stack.size(); i++)
S.O.P.("["+getVal(i)+"]"); //where getVal() will retrieve value at that index
mutex.signal();
}
}
类示例{
公共静态void main(){
//创建线程对象
StackPop p1=新的StackPop();
StackPop p2=新的StackPop();
StackPop p3=新的StackPop();
StackPush ps1=新的StackPush();
StackPush ps2=新的StackPush();
StackPush ps3=新的StackPush();
StackValues s1=新的StackValues();
StackValues s2=新的StackValues();
StackValues s3=新的StackValues();
//然后我们以混合顺序开始这些线程
p1.开始();
s3.start();
ps2.start();
//等
}
}
类StackPop扩展线程{
公开募捐{
mutex.wait();
pop();
SOP(“弹出的值为”+get.popVal);
mutex.signal();
}
}
类StackPush扩展线程{
公开募捐{
mutex.wait();
推();
SOP(“插入值为”+get.pushVal);
mutex.signal();
}
}
类StackValues扩展线程{
公开募捐{
mutex.wait();
对于(inti=0;i,您可以在关键部分使用锁或“synchronized”
您可以使用相同的想法来pop()push()等
具有互斥
try {
mutex.acquire();
try
{
// work
} finally {
mutex.release();
}
} catch(InterruptedException ie) {// ...}
来源->––第4部分
我有三种类型的线程:一种在堆栈上执行pop(),另一种在同一堆栈上执行push(),最后一种打印该堆栈的值
每个线程使用3个不同的堆栈,这就是为什么会出现意外情况。在一个堆栈上弹出,从不同的堆栈中推送,最后从另一个堆栈打印
你应该这样做:
public class Teste {
Thread t1;
Thread t2;
Thread t3;
Stack<Integer> stack = new Stack <Integer>();
private final Semaphore mutex = new Semaphore(1);
public Teste ()
{
Runnable r1 = new Runnable()
{
public void run()
{
pop();
} // run()
}; // runnable
Runnable r2 = new Runnable()
{
public void run()
{
for(int x = 0; x < 3; x++)
push(x);
} // run()
}; // runnable
Runnable r3 = new Runnable()
{
public void run()
{
for(int x = 0; x < 3; x++)
print();
} // run()
}; // runnable
this.t1 = new Thread(r1); // Create the thread associating the runnable
this.t2 = new Thread(r2);
this.t3 = new Thread(r3);
}
public void pop()
{
try {
mutex.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
stack.pop();
mutex.release();
}
// Similar to pop
public void push(int x){ //..}
public void print(){
try {
mutex.acquire();
} catch (InterruptedException e) {e.printStackTrace();}
for(int i = 0; i<stack.size(); i++)
System.out.println("["+stack.get(i)+"]");
mutex.release();
}
}
public static void main(String argv[])
{
Teste t = new Teste();
t.t2.start();
t.t1.start();
t.t3.start();
}
公共类测试{
螺纹t1;
螺纹t2;
螺纹t3;
堆栈=新堆栈();
私有最终信号量互斥=新信号量(1);
公共测试()
{
Runnable r1=新的Runnable()
{
公开募捐
{
pop();
}//运行()
};//可运行
Runnable r2=新的Runnable()
{
公开募捐
{
对于(int x=0;x<3;x++)
推(x);
}//运行()
};//可运行
Runnable r3=新的Runnable()
{
公开募捐
{
对于(int x=0;x<3;x++)
打印();
}//运行()
};//可运行
this.t1=新线程(r1);//创建与可运行线程关联的线程
this.t2=新螺纹(r2);
this.t3=新螺纹(r3);
}
公共空间
{
试一试{
mutex.acquire();
}捕捉(中断异常e){
e、 printStackTrace();
}
stack.pop();
mutex.release();
}
//类似流行音乐
公共无效推送(int x){/..}
公开作废印刷品(){
试一试{
mutex.acquire();
}catch(InterruptedException e){e.printStackTrace();}
对于(inti=0;i,您可以在关键部分使用锁或“synchronized”
您可以使用相同的想法来pop()push()等
具有互斥
try {
mutex.acquire();
try
{
// work
} finally {
mutex.release();
}
} catch(InterruptedException ie) {// ...}
来源->––第4部分
我有三种类型的线程:一种在堆栈上执行pop(),另一种在同一堆栈上执行push(),最后一种打印该堆栈的值
每个线程使用3个不同的堆栈,这就是为什么会出现意外情况。在一个堆栈上弹出,从不同的堆栈中推送,最后从另一个堆栈打印
你应该这样做:
public class Teste {
Thread t1;
Thread t2;
Thread t3;
Stack<Integer> stack = new Stack <Integer>();
private final Semaphore mutex = new Semaphore(1);
public Teste ()
{
Runnable r1 = new Runnable()
{
public void run()
{
pop();
} // run()
}; // runnable
Runnable r2 = new Runnable()
{
public void run()
{
for(int x = 0; x < 3; x++)
push(x);
} // run()
}; // runnable
Runnable r3 = new Runnable()
{
public void run()
{
for(int x = 0; x < 3; x++)
print();
} // run()
}; // runnable
this.t1 = new Thread(r1); // Create the thread associating the runnable
this.t2 = new Thread(r2);
this.t3 = new Thread(r3);
}
public void pop()
{
try {
mutex.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
stack.pop();
mutex.release();
}
// Similar to pop
public void push(int x){ //..}
public void print(){
try {
mutex.acquire();
} catch (InterruptedException e) {e.printStackTrace();}
for(int i = 0; i<stack.size(); i++)
System.out.println("["+stack.get(i)+"]");
mutex.release();
}
}
public static void main(String argv[])
{
Teste t = new Teste();
t.t2.start();
t.t1.start();
t.t3.start();
}
公共类测试{
螺纹t1;
螺纹t2;
螺纹t3;
堆栈=新堆栈();
私有最终信号量互斥=新信号量(1);
公共测试()
{
Runnable r1=新的Runnable()
{
公开募捐
{
pop();
}//运行()
};//可运行
Runnable r2=新的Runnable()
{
公开募捐
{
对于(int x=0;x<3;x++)
推(x);
}//运行()
};//可运行
Runnable r3=新的Runnable()
{
公开募捐
{
对于(int x=0;x<3;x++)
打印();
}//运行()
};//可运行
this.t1=新线程(r1);//创建与可运行线程关联的线程
this.t2=新螺纹(r2);
this.t3=新螺纹(r3);
}
公共空间
{
试一试{
mutex.acquire();
}捕捉(中断异常e){
e、 printStackTrace();
}
stack.pop();
mutex.release();
}
//类似流行音乐
公共无效推送(int x){/..}
公开作废印刷品(){
试一试{
mutex.acquire();
}catch(InterruptedException e){e.printStackTrace();}
对于(inti=0;i要小心,wait是从对象继承的,它的对立面是notify-您可能调用了错误的方法
另外,Java有一个本机同步机制,synchronized关键字,您可能需要对此进行研究。小心,wait是从对象继承的,它的反面是notify-您可能调用了错误的方法
而且,