discussion
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| discussion [2007/10/29 21:12] – cs253184 | discussion [2007/11/23 13:46] (current) – franck | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| Comments, remarks, suggestions, | Comments, remarks, suggestions, | ||
| - | ====== Slawomir' | ||
| - | Please download the {{SleepingBarber.jar|jar}} (the source code is in the jar), then run: | + | ===== Slawomir' |
| + | |||
| + | |||
| + | Please download the [[http:// | ||
| '' | '' | ||
| Line 14: | Line 16: | ||
| <code java> | <code java> | ||
| public class Barber extends Thread | public class Barber extends Thread | ||
| - | { | + | |
| - | ... | + | ... |
| - | public void run( ) | + | public void run( ) |
| - | { | + | { |
| - | try | + | try |
| - | { | + | { |
| - | while ( true ) | + | while ( true ) |
| - | { | + | { |
| - | synchronized ( shop ) | + | synchronized ( shop ) |
| - | { | + | { |
| - | while ( !shop.isCustomerWaiting( ) ) | + | while ( !shop.isCustomerWaiting( ) ) |
| - | shop.wait( ); | + | shop.wait( ); |
| - | if ( isAsleep ) | + | if ( isAsleep ) |
| - | { | + | { |
| - | isAsleep = false; | + | isAsleep = false; |
| - | setMessage( "awake ...", "wakes up" ); | + | setMessage( "awake ...", "wakes up" ); |
| - | shop.isCuttingChairBusy( false ); | + | shop.isCuttingChairBusy( false ); |
| - | setMessage( "ready ...", "frees his chair" ); | + | setMessage( "ready ...", "frees his chair" ); |
| - | } | + | } |
| - | shop.notifyAll( ); | + | shop.notifyAll( ); |
| - | setMessage( " | + | |
| - | while ( !shop.isCuttingChairBusy( ) ) | + | while ( !shop.isCuttingChairBusy( ) ) |
| - | shop.wait( ); | + | shop.wait( ); |
| - | } | + | } |
| - | Customer customer = shop.getCurrentCustommer( ); | + | |
| - | setMessage( " | + | setMessage( " |
| - | sleep( SleepingBarber.getRandomInt( SleepingBarber.MAX_TIME_HAIRCUT ) ); | + | sleep( SleepingBarber.getRandomInt( SleepingBarber.MAX_TIME_HAIRCUT ) ); |
| - | setMessage( " | + | setMessage( " |
| - | setMessage( " | + | setMessage( " |
| - | synchronized ( shop ) | + | |
| - | { | + | { |
| - | shop.letCurrentCustomerOut( ); | + | shop.letCurrentCustomerOut( ); |
| - | shop.notifyAll( ); | + | shop.notifyAll( ); |
| - | while ( shop.isCuttingChairBusy( ) ) | + | while ( shop.isCuttingChairBusy( ) ) |
| - | shop.wait( ); | + | shop.wait( ); |
| - | shop.currentCustomerDone( ); | + | shop.currentCustomerDone( ); |
| - | if ( !shop.isCustomerWaiting( ) ) | + | if ( !shop.isCustomerWaiting( ) ) |
| - | { | + | { |
| - | shop.isCuttingChairBusy( true ); | + | shop.isCuttingChairBusy( true ); |
| - | setMessage( " | + | setMessage( " |
| - | isAsleep = true; | + | isAsleep = true; |
| - | setMessage( " | + | setMessage( " |
| - | setMessage( " | + | setMessage( " |
| - | setMessage( " | + | setMessage( " |
| - | } | + | } |
| - | } | + | } |
| - | } | + | } |
| - | } | + | } |
| - | catch ( InterruptedException e ) | + | catch ( InterruptedException e ) |
| - | {} | + | {} |
| - | } | + | } |
| - | ... | + | ... |
| - | } | + | } |
| </ | </ | ||
| Line 76: | Line 78: | ||
| <code java> | <code java> | ||
| public class Customer extends Thread | public class Customer extends Thread | ||
| - | { | + | |
| - | ... | + | ... |
| - | public void run( ) | + | public void run( ) |
| - | { | + | { |
| - | try | + | try |
| - | { | + | { |
| - | while ( true ) | + | while ( true ) |
| - | { | + | { |
| - | setMessage( "away ...", " | + | setMessage( "away ...", " |
| - | setMessage( "away ...", "is away" ); | + | setMessage( "away ...", "is away" ); |
| - | setMessage( "away ...", " | + | setMessage( "away ...", " |
| - | sleep( SleepingBarber.getRandomInt( SleepingBarber.MAX_TIME_AWAY ) ); | + | sleep( SleepingBarber.getRandomInt( SleepingBarber.MAX_TIME_AWAY ) ); |
| - | synchronized ( shop ) | + | |
| - | { | + | { |
| - | setMessage( " | + | setMessage( " |
| - | int number = shop.getNumber( ); | + | int number = shop.getNumber( ); |
| - | if ( number != Shop.NO_ROOM ) | + | if ( number != Shop.NO_ROOM ) |
| - | { | + | { |
| - | if ( shop.isBarberAsleep( ) ) | + | if ( shop.isBarberAsleep( ) ) |
| - | { | + | { |
| - | setMessage( " | + | setMessage( " |
| - | shop.notifyAll( ); | + | shop.notifyAll( ); |
| - | } | + | } |
| - | if ( number != shop.nextServing( ) ) | + | if ( number != shop.nextServing( ) ) |
| - | setMessage( " | + | setMessage( " |
| - | else | + | else |
| - | setMessage( " | + | setMessage( " |
| - | while ( shop.isBarberAsleep( ) || shop.isCuttingChairBusy( ) || !shop.isCurrentCustomerDone( ) | + | while ( shop.isBarberAsleep( ) || shop.isCuttingChairBusy( ) || !shop.isCurrentCustomerDone( ) |
| - | || number != shop.nextServing( ) ) | + | || number != shop.nextServing( ) ) |
| - | shop.wait( ); | + | shop.wait( ); |
| - | setMessage( " | + | |
| - | shop.letCurrentCustomerIn( this ); | + | shop.letCurrentCustomerIn( this ); |
| - | shop.notifyAll( ); | + | shop.notifyAll( ); |
| - | setMessage( " | + | |
| - | while ( !shop.isCuttingDone( ) ) | + | while ( !shop.isCuttingDone( ) ) |
| - | shop.wait( ); | + | shop.wait( ); |
| - | setMessage( " | + | setMessage( " |
| - | shop.isCuttingChairBusy( false ); | + | shop.isCuttingChairBusy( false ); |
| - | shop.notifyAll( ); | + | shop.notifyAll( ); |
| - | setMessage( "done ...", "out cutting chair" ); | + | setMessage( "done ...", "out cutting chair" ); |
| - | } | + | } |
| - | } | + | } |
| - | } | + | } |
| - | } | + | |
| - | catch ( InterruptedException e1 ) | + | catch ( InterruptedException e1 ) |
| - | {} | + | {} |
| - | } | + | } |
| - | ... | + | ... |
| - | } | + | } |
| </ | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== Sleeping Barber using Semaphore===== | ||
| + | |||
| + | Since we have just covered semaphore in the class, here is a solution to the Sleeping Barber problem using semaphores. Althought it may looks awkward using semaphores in this problem, | ||
| + | I found it a good exerciese though. Also, I intentionally implemented the semaphors class, for practice and demonstration purpose. | ||
| + | |||
| + | <code java> | ||
| + | / | ||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | ****************************************************************************/ | ||
| + | public class Main | ||
| + | { | ||
| + | public static void main(String[] args) | ||
| + | { | ||
| + | final int numChairs = 5; | ||
| + | final int numCustomers = 10; | ||
| + | |||
| + | Shop shop = new Shop(numChairs); | ||
| + | |||
| + | | ||
| + | | ||
| + | |||
| + | for ( int i = 1; i <= numCustomers; | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | | ||
| + | } | ||
| + | } | ||
| + | |||
| + | / | ||
| + | / semaphore class | ||
| + | **************************************************/ | ||
| + | class MySemaphore | ||
| + | { | ||
| + | | ||
| + | |||
| + | | ||
| + | { | ||
| + | this.value = 0; | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | this.value = init; | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | this.value++; | ||
| + | if (this.value <= 1) // oringinally 0 | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | while (this.value == 0) | ||
| + | { | ||
| + | try{ | ||
| + | | ||
| + | } | ||
| + | catch (InterruptedException ie) {} | ||
| + | } | ||
| + | this.value--; | ||
| + | } | ||
| + | |||
| + | } // end of MySemaphore | ||
| + | |||
| + | / | ||
| + | / Shop class | ||
| + | **************************************************/ | ||
| + | class Shop | ||
| + | { | ||
| + | // attributes | ||
| + | private int numChairs ; | ||
| + | private int waitingCount ; | ||
| + | | ||
| + | private MySemaphore customers; | ||
| + | private MySemaphore barber; | ||
| + | private MySemaphore mutex; | ||
| + | private MySemaphore cutting; | ||
| + | | ||
| + | private int who; // who is in service room | ||
| + | | ||
| + | // constructor | ||
| + | public Shop(int nChairs) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | |||
| + | | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | while (true) | ||
| + | { | ||
| + | | ||
| + | if (waitingCount == 0) | ||
| + | System.out.println(" | ||
| + | |||
| + | | ||
| + | | ||
| + | + waitingCount); | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | try | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | catch (InterruptedException ie) | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | |||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | System.out.print( i + " coming" | ||
| + | this.mutex.waitMS(); | ||
| + | if ( waitingCount < numChairs ) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | try | ||
| + | { | ||
| + | Thread.sleep(2000)); | ||
| + | } | ||
| + | catch (InterruptedException ie) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | | ||
| + | | ||
| + | |||
| + | } | ||
| + | | ||
| + | else | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } // end of Shop | ||
| + | |||
| + | |||
| + | / | ||
| + | / Barber class | ||
| + | **************************************************/ | ||
| + | class Barber extends Thread | ||
| + | { | ||
| + | | ||
| + | |||
| + | | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | catch (InterruptedException ie) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | | ||
| + | shop.barberWorking(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | / | ||
| + | / Customer class | ||
| + | **************************************************/ | ||
| + | class Customer extends Thread | ||
| + | { | ||
| + | | ||
| + | | ||
| + | |||
| + | | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | catch (InterruptedException ie) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | | ||
| + | shop.customerWorking(this.id); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== Cigarette-Smokers problem ===== | ||
| + | Cigarette-Smokers problem is another famous thread synchronization problem in computer science. | ||
| + | |||
| + | Consider a system with three smoker processes and one agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and smoke a cigarette, the smoker needs three ingredients: | ||
| + | the agent on completion. The agent then puts out another two of the three ingredients, | ||
| + | |||
| + | Given the discussions and exercises in class, the implementation should look quite straightforward. | ||
| + | |||
| + | <code java> | ||
| + | / | ||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | ****************************************************************************/ | ||
| + | |||
| + | import java.util.*; | ||
| + | |||
| + | public class Main | ||
| + | { | ||
| + | | ||
| + | { | ||
| + | Table table = new Table(); | ||
| + | | ||
| + | Agent agent = new Agent(table); | ||
| + | Smoker smkr0 = new Smoker(" | ||
| + | Smoker smkr1 = new Smoker(" | ||
| + | Smoker smkr2 = new Smoker(" | ||
| + | |||
| + | // starting the threads | ||
| + | agent.start(); | ||
| + | smkr0.start(); | ||
| + | smkr1.start(); | ||
| + | smkr2.start(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | / | ||
| + | * class Table | ||
| + | ************************************************************************/ | ||
| + | class Table | ||
| + | { | ||
| + | // attributes | ||
| + | public static final int Tobacco = 0; | ||
| + | public static final int Paper = 1; | ||
| + | public static final int Matches = 2; | ||
| + | | ||
| + | private static final int Everything = Tobacco + Paper + Matches; | ||
| + | private static final int Nothing = -1; | ||
| + | public static final int END_TOKEN | ||
| + | |||
| + | private int onTable; | ||
| + | | ||
| + | // constructor | ||
| + | public Table () | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | | ||
| + | public synchronized void put(int put) | ||
| + | { | ||
| + | | ||
| + | { | ||
| + | case 1: System.out.println(" | ||
| + | case 2: System.out.println(" | ||
| + | case 3: System.out.println(" | ||
| + | default: System.out.print(" | ||
| + | } | ||
| + | |||
| + | | ||
| + | |||
| + | if ( put != END_TOKEN ) | ||
| + | | ||
| + | notifyAll(); | ||
| + | try { | ||
| + | wait(); } | ||
| + | catch (InterruptedException e) {...} | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | notifyAll(); | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | | ||
| + | while ( ( this.onTable + myHave) != Everything && this.onTable != END_TOKEN ) | ||
| + | { | ||
| + | try{ | ||
| + | | ||
| + | } | ||
| + | catch (InterruptedException e) {...} | ||
| + | } | ||
| + | |||
| + | if (this.onTable != END_TOKEN) | ||
| + | { | ||
| + | this.onTable = Nothing; | ||
| + | |||
| + | // smoking | ||
| + | | ||
| + | try | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | catch (InterruptedException ie){...} | ||
| + | |||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | } | ||
| + | else // read a END_TOKEN -100 | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | } // end of method | ||
| + | |||
| + | } // end of Agent class | ||
| + | |||
| + | |||
| + | / | ||
| + | * class Smoker | ||
| + | ************************************************************************/ | ||
| + | class Smoker extends Thread | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | |||
| + | | ||
| + | { | ||
| + | this.id = sid; | ||
| + | this.table = tab; | ||
| + | this.myHave = mh; | ||
| + | } | ||
| + | |||
| + | | ||
| + | | ||
| + | int returnValuel; | ||
| + | |||
| + | while (true) | ||
| + | { | ||
| + | int returnValue = this.table.getAndSmoke(this.id, | ||
| + | if ( returnValue == -100) // read a end token, done | ||
| + | break; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } // end of table | ||
| + | |||
| + | / | ||
| + | * class Agent | ||
| + | ************************************************************************/ | ||
| + | |||
| + | class Agent extends Thread | ||
| + | { | ||
| + | | ||
| + | |||
| + | | ||
| + | { | ||
| + | this.table = tab; | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | Random rand = new Random(); | ||
| + | int putStuff1, putStuff2; | ||
| + | int i = 0; | ||
| + | |||
| + | while (i < 10) // put 10 times then finish | ||
| + | { | ||
| + | do | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | while ( putStuff1 == putStuff2 ); // until different value | ||
| + | | ||
| + | System.out.println(putStuff1 + " + " + putStuff2); | ||
| + | this.table.put( putStuff1 + putStuff2); | ||
| + | i++; | ||
| + | } | ||
| + | | ||
| + | // finally , put END_TOKEN | ||
| + | | ||
| + | |||
| + | } | ||
| + | |||
| + | | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Franck' | ||
| + | |||
| + | Below you find the code that I used to schedule the third presentations. | ||
| + | |||
| + | <code java> | ||
| + | import java.util.*; | ||
| + | import java.io.*; | ||
| + | |||
| + | /** | ||
| + | * Prints out the schedule of the third presentations. | ||
| + | * | ||
| + | * @author Franck van Breugel | ||
| + | */ | ||
| + | public class Presentation | ||
| + | { | ||
| + | /** | ||
| + | * Prints out the schedule of the third presentations. | ||
| + | * | ||
| + | * @param args none. | ||
| + | * @throws Exception if file cannot be found. | ||
| + | */ | ||
| + | public static void main(String[] args) throws Exception | ||
| + | { | ||
| + | final int NUMBER_OF_STUDENTS = 16; | ||
| + | final String FILE_NAME = " | ||
| + | final int MAX = 1000; | ||
| + | final int LECTURES = 4; | ||
| + | final int NUMBER_PER_LECTURE = 4; | ||
| + | |||
| + | List< | ||
| + | Scanner input = new Scanner(new File(FILE_NAME)); | ||
| + | while (input.hasNext()) | ||
| + | { | ||
| + | String[] record = input.nextLine().split("," | ||
| + | names.add(record[1].trim() + " " + record[0].trim()); | ||
| + | } | ||
| + | |||
| + | Random random = new Random(); | ||
| + | int number = random.nextInt(MAX); | ||
| + | for (int i = 0; i < number; i++) | ||
| + | { | ||
| + | Collections.shuffle(names); | ||
| + | } | ||
| + | |||
| + | for (int i = 0; i < LECTURES; i++) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | for (int j = 0; j < NUMBER_PER_LECTURE | ||
| + | { | ||
| + | System.out.printf(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
discussion.1193692342.txt.gz · Last modified: (external edit)
