Contents

  1. .
  2. ./index.html
  3. ./PingPong.java

. 1/3

[
top][prev][next]


./index.html 2/3

[
top][prev][next]
<html>
<head><title>Examples for /home/faculty/sprenkle/public_html/cs330/examples/23-syncwrapup</title>
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/assignments/assign.css" />
</head>
<body>
<h1>Examples for /home/faculty/sprenkle/public_html/cs330/examples/23-syncwrapup</h1>
<ul>
<li><a href=".//code.html">All IN ONE FILE (pretty syntax)</a>
<li><a href=".//PingPong.java">PingPong.java</a></li>
</ul>
</body>

./PingPong.java 3/3

[
top][prev][next]
//package pingpong;

/**
 * Need a shared lock to synchronize PingPong
 * 
 * @author Sara Sprenkle
 *
 */
public class PingPong {
	
	public static Object sharedLock;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	    // to make easier to read...
		PingPongThread ping = new PingPongThread("PING");
		PingPongThread pong = new PingPongThread("pong");
		sharedLock = new Object();
		
		ping.start();
		pong.start();
	}
	
}


class PingPongThread extends Thread {
	
	private String id;
	
	public PingPongThread(String id) {
		this.id = id;
	}
	
	@Override
	public void run() {
		while(true) {
			synchronized (PingPong.sharedLock) {
				System.out.println(id);
				PingPong.sharedLock.notify();
				try {
					PingPong.sharedLock.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}


Generated by GNU Enscript 1.6.6.