added ex I
This commit is contained in:
		
							
								
								
									
										32
									
								
								src/exercicses/ex_i/Employee.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/exercicses/ex_i/Employee.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| package exercicses.ex_i; | ||||
|  | ||||
| public class Employee extends Thread { | ||||
|     private String name; | ||||
|     private String room; | ||||
|     private int exageratedTimeUsageFactor; | ||||
|  | ||||
|     public Employee(String name, String room, int exageratedTimeUsageFactor) { | ||||
|         this.name = name; | ||||
|         this.room = room; | ||||
|         this.exageratedTimeUsageFactor = exageratedTimeUsageFactor; | ||||
|     } | ||||
|  | ||||
|     public void useRoom(int time) { | ||||
|         synchronized (room) { | ||||
|             System.out.println(name + " uses room for time: " + time); | ||||
|             try { | ||||
|                 Thread.sleep(time); | ||||
|             } catch (InterruptedException e) { | ||||
|                 throw new RuntimeException(e); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void run() { | ||||
|         System.out.println(name + " thread started"); | ||||
|         while (true) { | ||||
|             useRoom(100 * exageratedTimeUsageFactor); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										13
									
								
								src/exercicses/ex_i/TestingStarvation.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/exercicses/ex_i/TestingStarvation.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| package exercicses.ex_i; | ||||
|  | ||||
| public class TestingStarvation { | ||||
|     public static void main(String[] args) { | ||||
|         String sharedRoom = "Polaris"; | ||||
|         Employee t1 = new Employee("Emma", sharedRoom, 1); | ||||
|         Employee t2 = new Employee("Jean", sharedRoom, 100); | ||||
|  | ||||
|         t1.start(); | ||||
|         t2.start(); | ||||
|         System.out.println("Main thread ended"); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user