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