discussion
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| discussion [2007/11/01 18:34] – huiwang | discussion [2007/11/23 13:46] (current) – franck | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| - | ====== Slawomir' | + | ===== Slawomir' |
| Line 136: | Line 136: | ||
| - | ====== Sleeping Barber using Semaphore====== | + | |
| + | ===== 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, | 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, | ||
| Line 373: | Line 374: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== 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.1193942076.txt.gz · Last modified: by huiwang
