Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

java - 如何查找为什么我的Java信号不起作用(How to find why my Java semaphore is not working)

I wrote codes for traffic rules with thread and semaphore.

(我用线程和信号量编写了交通规则代码。)

I think my codes are ok but it is not working.

(我认为我的代码还可以,但是没有用。)

I mean it is not lock my second thread what I wrote under of my first thread.

(我的意思是它并没有锁定我在第一个线程下编写的内容。)

How can I find my error?

(如何找到我的错误?)

why this not lock my second thread?

(为什么这不能锁定我的第二个线程?)

I want to see that result: I'll have 6 car in the traffic.

(我想看一下结果:我要开6辆车。)

3 car is on one way line (with different coordinate) and another 3 car is on another way line (with different coordinate).

(3辆汽车在一条路线(坐标不同)上,另一辆3辆在另一条路线(坐标不同)上。)

For example A coordinate is 5, B is 10 and C is 15.

(例如,坐标为5,B为10,C为15。)

Conversely, D is -6, E is -11 and F is -16.

(相反,D为-6,E为-11,F为-16。)

There are way crossing.

(有过路处。)

if A,B and C car points are decreasing (every two second they coordinate decrease 1 by 1).

(如果A,B和C的乘车点正在减少(每两秒协调1减1)。)

They want to reach to "upTarget" for example 3. and D,E,F coordinate increasing one by one to reach -3.

(例如,他们想到达“ upTarget”。3,并且D,E,F坐标一一增加到-3。)

if A is reach to uptarget (to 3) before D reach his leftarget(to -3), all left line will wait until upline was leave 0,0 coordinate

(如果A在D到达其左箭头(至-3)之前已到达向上目标(至3),则所有左行将一直等到上线离开0,0坐标)

I mean if A will reach to 3 then ABC line will be green light to go out and D,E,F will wait them until C coordinate will be 0

(我的意思是,如果A会达到3,那么ABC线会亮绿灯,而D,E,F将等到它们直到C坐标为0)

MainClass

(主类)

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();
    }


}

My backend codes => my thread class

(我的后端代码=>我的线程类)

   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();
        }    }
}
  ask by Mehman Hummetli translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You code is working fine, the semaphore is working.

(您的代码工作正常,信号量正常。)

Please change the while loop of thread B in your code to add more system.out.println() as below and see that while semaphore is acquired by thread A, Thread B is actually hanged (as expected).

(请在代码中更改线程B的while循环,以添加更多的system.out.println(),如下所示,并看到虽然线程A获取了信号量,但实际上线程B已挂起(如预期的那样)。)


                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!!!");
                    }
                }

If you were expecting something else, then please clarify.

(如果您还有其他期望,请澄清。)

If this answer was helpful, don't forget to upvote!

(如果这个答案有帮助,请不要忘记投票!)

**********UPDATE*********

(**********更新*********)

Based on your comment, I think what you can do is add another if condition to check if semaphore has been acquired by the other thread or not, before changing coordinates as follows;

(根据您的评论,我认为您可以做的是在更改坐标之前,添加另一个if条件,以检查信号量是否已被其他线程获取;)

First set a static synchronised variable to show which thread acquired semaphore first.

(首先设置一个静态同步变量,以显示哪个线程首先获取了信号量。)

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();
            }    }
    }

This should work!

(这应该工作!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...