Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.*;
- import java.util.concurrent.atomic.*;
- public class Elevator{
- private int floor;
- int current_floor=0;
- AtomicBoolean busy= new AtomicBoolean(false);
- Elevator(int num_floor){
- floor= 0;
- }
- public void call(int floor){
- new Thread(){
- public void run(){
- synchronized(busy){
- while(busy.get()){
- try{
- System.out.println("Call in wait");
- busy.wait();
- }
- catch(InterruptedException ex){}
- move(floor);
- }
- move(floor);
- }
- }
- }.start();
- }
- public void move(int floor){
- synchronized(busy){
- while(busy.get()){
- try{
- busy.wait();
- }
- catch(InterruptedException ex){}
- }
- try{
- busy.set(true);
- System.out.println("Leaving floor "+ current_floor);
- Thread.currentThread().sleep(2000);
- current_floor=floor;
- System.out.println("Reaching floor "+ current_floor);
- busy.set(false);
- busy.notify();
- }
- catch(InterruptedException ex){}
- }
- }
- public static void main(String args[]){
- Elevator e= new Elevator(10);
- e.call(1);
- e.call(2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement