Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab4Filo;
- public class Filozof extends Thread{
- Bat stg,dr;
- int id;
- public Filozof(Bat st, Bat dr,int id){
- this.stg = st;
- this.dr = dr;
- this.id = id;
- }
- public void run(){
- while(true){
- if(stg.bat == false || dr.bat == false){
- synchronized(stg){
- try{
- System.out.println("Filozoful " + id +" asteapta!");
- sleep(700);
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- synchronized(dr){
- try{
- //System.out.println("Filozoful " + id +" asteapta!");
- sleep(400);
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- if(dr.bat == true){
- dr.bat = false;
- synchronized(dr){
- if(stg.bat == true){
- stg.bat = false;
- synchronized(stg){
- try{
- sleep(1000);
- }
- catch(InterruptedException e){
- e.printStackTrace();
- }
- System.out.println("Filozoful " + id +" a mancat!");
- dr.bat = true;
- stg.bat = true;
- System.out.println("Filozoful " + id +" mediteaza!");
- try{
- sleep(1000);
- }
- catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement