Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何找到我的Java信号量不工作的原因_Java_Multithreading_Semaphore - Fatal编程技术网

如何找到我的Java信号量不工作的原因

如何找到我的Java信号量不工作的原因,java,multithreading,semaphore,Java,Multithreading,Semaphore,我用线程和信号量为交通规则编写代码 我认为我的代码是好的,但它不工作。我的意思是,这不是锁定我的第二个线程,我写下了我的第一个线程。我怎样才能找到我的错误?为什么不锁定我的第二个线程 我希望看到这样的结果: 我会有6辆车在路上。3辆车在一条路线上(坐标不同),另3辆车在另一条路线上(坐标不同)。例如,A坐标为5,B坐标为10,C坐标为15 相反,D为-6,E为-11,F为-16。有两条路交叉。如果A、B和C车点在减少(每两秒钟它们的坐标减少1乘1)。例如,他们想要达到“上升目标”。D,E,F坐标

我用线程和信号量为交通规则编写代码

我认为我的代码是好的,但它不工作。我的意思是,这不是锁定我的第二个线程,我写下了我的第一个线程。我怎样才能找到我的错误?为什么不锁定我的第二个线程

我希望看到这样的结果: 我会有6辆车在路上。3辆车在一条路线上(坐标不同),另3辆车在另一条路线上(坐标不同)。例如,A坐标为5,B坐标为10,C坐标为15

相反,D为-6,E为-11,F为-16。有两条路交叉。如果A、B和C车点在减少(每两秒钟它们的坐标减少1乘1)。例如,他们想要达到“上升目标”。D,E,F坐标依次增加,达到-3。如果A在D到达其左目标(到-3)之前到达上目标(到3),所有左线将等待直到上线离开0,0坐标


我的意思是,如果A达到3,那么bc线将是绿灯,D,E,F将等待它们,直到C坐标为0

主类

package sample;

import java.util.Scanner;
import java.util.concurrent.Semaphore;

public class Main {
    public static int x1,x2,x3,y1,y2,y3,upTarget,leftTarget;

    public static void main(String[] args) throws InterruptedException{

        Scanner obj=new Scanner(System.in);

        System.out.println("Insert coordinant of A");
        x1=obj.nextInt();
        System.out.println("Insert coordinant of B");
        x2=obj.nextInt();
        System.out.println("Insert coordinant of C");
        x3=obj.nextInt();

        System.out.println("Insert coordinant of D");
        y1=obj.nextInt();
        System.out.println("Insert coordinant of E");
        y2=obj.nextInt();
        System.out.println("Insert coordinant of F");
        y3=obj.nextInt();

        System.out.println("Insert up target level");
        upTarget=obj.nextInt();

        System.out.println("Insert left target level");
        leftTarget=obj.nextInt();

        System.out.println("Traffic was start");
        Semaphore sem=new Semaphore(1);


        Thread t1=new MyBackend(sem,x1, x2, x3, upTarget, y1, y2, y3, leftTarget,"A");
        Thread t2=new MyBackend(sem,x1, x2, x3, upTarget, y1, y2, y3, leftTarget,"B");





        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }


}
我的后端代码=>我的线程类

   import java.util.concurrent.Semaphore;

public class MyBackend extends Thread{
     Semaphore sem;
    private int x1,x2,x3,y1,y2,y3,upTarget,leftTarget;
    private String value;

    public MyBackend(Semaphore sem,int x1,int x2,int x3,int upTarget,int y1,int y2,int y3,int leftTarget,String value){
        super(value);
        this.sem=sem;
        this.x1=x1;
        this.x2=x2;
        this.x3=x3;
        this.y1=y1;
        this.y2=y2;
        this.y3=y3;
        this.upTarget=upTarget;
        this.leftTarget=leftTarget;
        this.value=value;
    }

    @Override
    public void run() {
        System.out.println("yeeeeeeeeeeeeeeeeeeeeeeeeeee"+sem.availablePermits());
        if(this.getName().equals("A")){
            try{

                while(x3>=0){
                    x1=x1-1;
                    x2=x2-1;
                    x3=x3-1;
                    Thread.sleep(2000);
                    System.out.println("A coordinate "+x1);
                    System.out.println("B coordinate "+x2);
                    System.out.println("C coordinate "+x3);

                    if(x1==upTarget){
                        sem.acquire();
                        System.out.println("yeeeeeeeeeeeeeeeeeeeeeeeeeee"+sem.availablePermits());
                        System.out.println("First line was reached!!!");
                    }
                }

            }catch(Exception e){}
            sem.release();
        }
        else {
            try{
                while(y3<=0){
                    y1=y1+1;
                    y2=y2+1;
                    y3=y3+1;
                    Thread.sleep(2000);
                    System.out.println("D coordinate "+y1);
                    System.out.println("E coordinate "+y2);
                    System.out.println("F coordinate "+y3);

                    if(y3==leftTarget){
                        sem.acquire();
                        System.out.println("Second line was reached!!!");
                    }
                }
            }catch(Exception e){}
            sem.release();
        }    }
}
import java.util.concurrent.Semaphore;
公共类MyBackend扩展线程{
信号量扫描电镜;
私有int x1,x2,x3,y1,y2,y3,向上目标,左目标;
私有字符串值;
公共MyBackend(信号量sem、int-x1、int-x2、int-x3、int-upTarget、int-y1、int-y2、int-y3、int-leftTarget、字符串值){
超级(价值);
这个.sem=sem;
这是x1=x1;
这个。x2=x2;
这个。x3=x3;
这个。y1=y1;
这个。y2=y2;
这个。y3=y3;
this.upTarget=upTarget;
this.leftTarget=leftTarget;
这个。值=值;
}
@凌驾
公开募捐{
System.out.println(“yeeeee”+sem.availablePermits());
if(this.getName().equals(“A”)){
试一试{
而(x3>=0){
x1=x1-1;
x2=x2-1;
x3=x3-1;
《睡眠》(2000年);
System.out.println(“A坐标”+x1);
系统输出打印项次(“B坐标”+x2);
系统输出打印项次(“C坐标”+x3);
如果(x1==upTarget){
sem.acquire();
System.out.println(“yeeeee”+sem.availablePermits());
System.out.println(“到达第一行!!!”;
}
}
}捕获(例外e){}
sem.release();
}
否则{
试一试{

(y3您的代码工作正常,但信号量工作正常。请在代码中更改线程B的while循环,以添加更多system.out.println(),如下所示,并查看线程A获取信号量时,线程B实际挂起(如预期)


而(y3=0){
if(acquiredBy!=null&&!acquiredBy.equals(this.getName())){
sem.acquire();
}
x1=x1-1;
x2=x2-1;
x3=x3-1;
《睡眠》(2000年);
System.out.println(“A坐标”+x1);
系统输出打印项次(“B坐标”+x2);
系统输出打印项次(“C坐标”+x3);
如果(x1==upTarget){
sem.acquire();
acquiredBy=this.getName();
System.out.println(“yeeeee”+sem.availablePermits());
System.out.println(“到达第一行!!!”;
}
}
}捕获(例外e){}
sem.release();
}
否则{
试一试{

while(y3)你在观察什么,你到底期望什么?注意分享任何输出!我将有6辆车在交通中。3辆车在一条路线上(坐标不同),另3辆车在另一条路线上(坐标不同)。例如A坐标为5,B坐标为10,C坐标为15。相反,D坐标为-6,E坐标为-11,F坐标为-16。存在道路交叉。如果A、B和C车辆点在减少(每两秒钟他们坐标减少1倍1)。他们希望达到“上升目标”,例如3。而D、E、F坐标则逐一增加以达到-3。如果A坐标达到上升目标(至3)在D到达他的左目标(到-3)之前,所有的左线将等待直到上线离开0,0坐标。我的意思是,如果A将到达3,那么B C线将绿灯熄灭,而D,E,F将等待他们直到C坐标为0,这将不起作用!我的目的是,如果x1=3,然后锁定第二条道路的车(D,E,F)然后继续第1行的工作。如果第1行中的最后一辆车(x3)等于0,那么松开D E F车的锁,该车将继续行驶。你明白我的意思了吗?用户给A B C D E F正确的数字5,10,15,-7,-12,-17。A,B,C车点将达到3(上升目标),反之,E,D,F希望达到-3(左目标)。另一种方法是,当哪条线路的车辆到达目标时,车辆协调将锁定。我根据您的评论更新了我的答案。请查看输出a坐标的某些部分4b坐标9d坐标-6e坐标-11f坐标-16c坐标14d坐标-5e坐标-10f坐标-15a坐标3b坐标8 C坐标13 Yeeeeeee0到达第一行!!!D坐标-4 E坐标-9 F坐标-14 A坐标2 B坐标7 C坐标12 A坐标1 B坐标6 C坐标11添加螺纹。sleep()到两个while循环的第一行。应该没问题。发生的事情是线程a已经获得了信号量,但同时线程b增加了d、e和f的坐标。如果不调用

                while(y3<=0){
                    y1=y1+1;
                    y2=y2+1;
                    y3=y3+1;
                    Thread.sleep(2000);
                    System.out.println("D coordinate "+y1);
                    System.out.println("E coordinate "+y2);
                    System.out.println("F coordinate "+y3);

                    if(y3==leftTarget){
                        System.out.println("Thread B acquiring sem: "+sem.availablePermits() + "If this is 0, D,E and F should hang");
                        sem.acquire();
                        System.out.println("Semaphore acquired, continue thread B for D, E and F"+sem.availablePermits());
                        System.out.println("Second line was reached!!!");
                    }
                }
import java.util.concurrent.Semaphore;

    public class MyBackend extends Thread{
         Semaphore sem;
        private volatile String acquiredBy = null;
        private int x1,x2,x3,y1,y2,y3,upTarget,leftTarget;
        private String value;

        public MyBackend(Semaphore sem,int x1,int x2,int x3,int upTarget,int y1,int y2,int y3,int leftTarget,String value){
            super(value);
            this.sem=sem;
            this.x1=x1;
            this.x2=x2;
            this.x3=x3;
            this.y1=y1;
            this.y2=y2;
            this.y3=y3;
            this.upTarget=upTarget;
            this.leftTarget=leftTarget;
            this.value=value;
        }

        @Override
        public void run() {
            System.out.println("yeeeeeeeeeeeeeeeeeeeeeeeeeee"+sem.availablePermits());
            if(this.getName().equals("A")){
                try{

                    while(x3>=0){
                        if(acquiredBy != null && !acquiredBy.equals(this.getName())){
                              sem.acquire();
                        }
                        x1=x1-1;
                        x2=x2-1;
                        x3=x3-1;
                        Thread.sleep(2000);
                        System.out.println("A coordinate "+x1);
                        System.out.println("B coordinate "+x2);
                        System.out.println("C coordinate "+x3);

                        if(x1==upTarget){
                            sem.acquire();
                            acquiredBy = this.getName();

                             System.out.println("yeeeeeeeeeeeeeeeeeeeeeeeeeee"+sem.availablePermits());
                            System.out.println("First line was reached!!!");
                        }
                    }

                }catch(Exception e){}
                sem.release();
            }
            else {
                try{
                    while(y3<=0){
                        if(acquiredBy != null &&  !acquiredBy.equals(this.getName())){
                              sem.acquire();
                        }
                        y1=y1+1;
                        y2=y2+1;
                        y3=y3+1;
                        Thread.sleep(2000);
                        System.out.println("D coordinate "+y1);
                        System.out.println("E coordinate "+y2);
                        System.out.println("F coordinate "+y3);

                        if(y3==leftTarget){
                            sem.acquire();
                            acquiredBy = this.getName();
                            System.out.println("Second line was reached!!!");
                        }
                    }
                }catch(Exception e){}
                sem.release();
            }    }
    }