Contents

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

. 1/4

[
top][prev][next]


./index.html 2/4

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

./PingPong.java 3/4

[
top][prev][next]
/*
 * PingPong
 * Threads coordinate on a static (shared) Object named lock
 * @author Sara Sprenkle
 */
public class PingPong implements Runnable {
    
    private String color;
    public static String turn = "purple";
    private static Object lock = new Object();
    
    public static void main(String args[]) {
        PingPong ping = new PingPong("purple");
        PingPong pong = new PingPong("blue");
        
        Thread one = new Thread(ping);   
        Thread two = new Thread(pong);   

        one.start(); two.start();
    }
    
    public PingPong(String color) {
        this.color = color;   
    }
    
    public void run() {
        pingPong();   
    }
    
    public void pingPong(){
        synchronized( lock ) {
            while(true) {
                System.out.println(color);
                lock.notify();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    System.out.println("oops!");   
                }
            }
        }
    }

}

./PingPongLock.java 4/4

[
top][prev][next]
/*
 * PingPongLock
 * 
 * Threads coordinate on a shared PingPongLock object that was passed
 * to each of them.  This version is closer to the theoretical version
 * from class.
 * 
 * @author Sara Sprenkle
 */
public class PingPongLock {

    public static void main( String[] args ) {
        PingPongLock lock = new PingPongLock();
        
        Thread t1 = new PPThread( "purple", lock);
        Thread t2 = new PPThread( "blue", lock );
        
        t1.start(); t2.start();
        
    }


    public synchronized void pingPong(String color) {
        while(true) {
            System.out.println(color);
            notify();
            try {
                wait();
            } catch (InterruptedException exc) {;}
        }
    }
    
}

class PPThread extends Thread {
    
    private String color;
    private PingPongLock lock;
    
    public PPThread( String color, PingPongLock lock ) {
        this.color = color;   
        this.lock = lock;
    }
    
    public void run() {
        lock.pingPong(color);   
    }
    
}


Generated by GNU Enscript 1.6.6.