Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By jlczuk

Thread: Thread and Synchronization

  1. #1
    yolo is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Smile Thread and Synchronization

    Question:
    The following class implements a model of a student dining hall serving pizzas to students. 10 pizzas are baked, then served to 20 students. Students are numbered between 0 and 19 in the order they are served. A message is printed indicating whether a student starved or was served a pizza.

    a. Rewrite the DiningHall class so that after the makePizza( ) method is called 10 times, the servePizza( ) method is called once each from 20 different threads.
    b. Insert synchronization to eliminate data races in your code, if any exist.
    c. Describe what data races may occur in your multithreaded code without synchronization.

    Java Code:
    public class DiningHall {
    	static int pizzaNum;
    	static int studentID;
    
    	public void makePizza() {
    		pizzaNum++;
    	}
    
    	public void servePizza() {
    		String result;
    		if (pizzaNum > 0) {
    			result = "Served ";
    			pizzaNum--;
    		} else
    			result = "Starved ";
    		System.out.println(result + studentID);
    		studentID++;
    	}
    
    	public static void main(String[] args) {
    		DiningHall d = new DiningHall();
    		for (int i = 0; i < 10; i++) 
    			d.makePizza();
    		for (int i = 0;i < 20; i++)
    			d.servePizza();
    	}
    }
    My Solution:
    Java Code:
    	public static void main(String[] args) {
    		DiningHall d = new DiningHall();
    		for (int i = 0; i < 10; i++) {
    			d.makePizza();
    			if (i == 9){
    				for (int j = 0;j < 20; j++)
    					d.servePizza();
    				}
    			}
    	}
    I have not idea on how to go about the data synchronization.
    Thank you

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Thread and Synchronization

    Where's the threads? Calling a servePizza() method 20 times from a for loop is only doing it sequentially.

    You should study the Processes and Threads tutorial, try to implement your threads and then come back and post questions you may have about your code.
    yolo likes this.

Similar Threads

  1. Some Thread Synchronization Question?
    By javakt in forum Threads and Synchronization
    Replies: 3
    Last Post: 06-11-2011, 05:58 PM
  2. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-18-2011, 12:30 PM
  3. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-07-2011, 07:38 AM
  4. Thread safe without using synchronization
    By swetu.vc in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-20-2010, 08:06 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •