DOC PREVIEW
UMD CMSC 330 - Sample concurrency questions from past exams

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 330 Sample concurrency questions from past exams Fall 2005Note: these were exam questions from prior semesters of CMSC 330, before Java 1.5 existed, thereforeall the questions and solutions use Java 1.4 synchronization. For additional practice you may also want toconsider how the questions or solutions could have been written using Java 1.5 s ynchronization.1. In this problem you are to modify the concurrent Java program on the next page to ensure that its twothreads always take turns in a hypothetical game they are playing.• The two threads playing the game are player1 (a Player1 object) and player2 (a Player2 object).• The game consists of turns in which each player makes a move (the exact details of the game arenot explained). The two players are supposed to alternate moves in the game, and the game shouldcontinue forever.• Each player thread determines its game move based on the other player’s previous move.• player1 should always make the first move in the game.• A move in the game is represented using an integer.• The two threads call methods named decide_next_move to determine their moves. After a threaddetermines its next move it communicates it to the other thread by calling set_move to store it ina shared Move object in the main function of the Game class. The other thread can examine thatvalue using get_move.• The function process_move is called by each thread to process the other thread’s most recentmove.Note that the code for the classes Player1 and Player2 are almost identical except the statements intheir run methods are in different orders. This is to cause player1 to make the first move in the game,and player2 to initially wait for player1’s first move.As the program is currently written the two threads do not use any synchronization, so incorrect orinconsistent results may be produced. Add appropriate Java synchronization constructs to the programso it always works properly. The conditions which must be ensured are:• Every consecutive move value should be stored and printed (as in the output fragment below).• The threads must strictly alternate their moves.• Only one thread may be making a move at a time.• The two threads cannot concurrently modify the value in the shared variable move.• Neither thread should store a value in move until the other thread has gotten the value which wasthere and has placed a new value there.• Neither thread should use the value in move until the other thread has stored a move in it.1As the program is written now, these conditions may not al-ways hold. In particular, nothing forces the threads to strictlyalternate their moves, and the other conditions may also beviolated.The rules of the game above must not be modified, so (be-yond adding what’s necessary to ensure these conditions) youshould not change player1’s or player2’s operations.Other than that, you may add anything necessary to theprogram to guarantee these conditions always hold, exceptthat busy–waiting may not be used.Note that when the program is corrected, its output will al-ways be the same any time it is run, beginning as shown tothe right.Player 1 begins game turnPlayer 1 decides move 1Player 2 processes move 1Player 2 begins game turnPlayer 2 decides move 2Player 1 processes move 2Player 1 begins game turnPlayer 1 decides move 3Player 2 processes move 3Player 2 begins game turnPlayer 2 decides move 4Player 1 processes move 42import java.io.*;public class Game {public static void main(String args[]) {Move move= new Move();Player1 player1= new Player1(move);Player2 player2= new Player2(move);player1.start();player2.start();}} // end Game class//////////////////////////////////////////class Player1 extends Thread {Move move;int value;Player1(Move m) {move= m;value= 0;}int decide_next_move(int prev_move) {return prev_move + 1;}void process_move(int m) {// perhaps some computation occurs// here which isn’t shownSystem.out.println("Player 1 processes move " + m);}public void run() {while (true) {System.out.println("\nPlayer 1 begins game turn");// compute next movevalue= decide_next_move(value);System.out.println("Player 1 decides move " + value);// communicate next move to Player2move.set_move(value);// get Player2’s move and process itvalue= move.get_move();process_move(value);} // while}} // end Player1 class3class Move {int move_value;void set_move(int new_move_value) {move_value= new_move_value;}int get_move() {return move_value;}} // end Move class//////////////////////////////////////////class Player2 extends Thread {Move move;int value;Player2(Move m) {move= m;}int decide_next_move(int prev_move) {return prev_move + 1;}void process_move(int m) {// perhaps some computation occurs// here which isn’t shownSystem.out.println("Player 2 processes move " + m);}public void run() {while (true) {// get Player1’s move and process itvalue= move.get_move();process_move(value);System.out.println("\nPlayer 2 begins game turn");// compute next movevalue= decide_next_move(value);System.out.println("Player 2 decides move " + value);// communicate next move to Player1move.set_move(value);} // while}} // end Player2 class42. Consider the Java program below, which does nothing useful but does use concurrency.import java.io.*;class ClockRadio {synchronized void snooze() {try {wait();} catch (Exception e) {// do nothing}}synchronized void alarm() {notify();}synchronized void big_alarm() {notifyAll();}} /////// end of ClockRadio classpublic class Main extends Thread {String name;ClockRadio cr;Main(String s, ClockRadio c) {name= s;cr= c;}public void run() {cr.snooze();System.out.print(name + " ");}public static void main(String args[]) {ClockRadio cr1= new ClockRadio();ClockRadio cr2= new ClockRadio();ClockRadio cr3= new ClockRadio();Main a= new Main("a", cr1);Main b= new Main("b", cr1);Main c= new Main("c", cr2);Main d= new Main("d", cr2);Main e= new Main("e", cr3);Main f= new Main("f", cr3);Main g= new Main("g", cr3);// HERE IS THE COMMENT}} ////////////// end of Main class5There is a comment at the end of the main func-tion of the Main class. The following questionsask for all of the possible outputs which couldbe produced, during different executions of theprogram, if groups of statements were insertedwhere the comment appears (and the main func-tion of the Main class was run). Note that alloutput produced during any execution wouldappear on a single line, since the only outputstatement used is a System.out.print().Any comment


View Full Document

UMD CMSC 330 - Sample concurrency questions from past exams

Documents in this Course
Exam #1

Exam #1

6 pages

Quiz #1

Quiz #1

2 pages

Midterm 2

Midterm 2

12 pages

Exam #2

Exam #2

7 pages

Ocaml

Ocaml

7 pages

Parsing

Parsing

38 pages

Threads

Threads

12 pages

Ruby

Ruby

7 pages

Quiz #3

Quiz #3

2 pages

Threads

Threads

7 pages

Quiz #4

Quiz #4

2 pages

Exam #2

Exam #2

6 pages

Exam #1

Exam #1

6 pages

Threads

Threads

34 pages

Quiz #4

Quiz #4

2 pages

Threads

Threads

26 pages

Exam #2

Exam #2

9 pages

Exam #2

Exam #2

6 pages

Load more
Download Sample concurrency questions from past exams
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Sample concurrency questions from past exams and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Sample concurrency questions from past exams 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?