Contents

  1. .
  2. ./BoundedBuffer.java
  3. ./CriticalSectionEx.java
  4. ./index.html
  5. ./SerializationExCond.java
  6. ./SerializationEx.java
  7. ./SerializationExSem.java

. 1/7

[
top][prev][next]


./BoundedBuffer.java 2/7

[
top][prev][next]

import java.util.concurrent.*;
import java.util.concurrent.locks.*;

/**
 * From https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Condition.html
 *
 */
class BoundedBuffer {
	final Lock lock = new ReentrantLock();
	final Condition notFull = lock.newCondition();
	final Condition notEmpty = lock.newCondition();
	final Object[] items = new Object[100];
	int putptr, takeptr, count;

	public void put(Object x) throws InterruptedException {
		lock.lock();
		try {
			while (count == items.length)
				notFull.await();
			items[putptr] = x;
			if (++putptr == items.length)
				putptr = 0;
			++count;
			notEmpty.signal();
		} finally {
			lock.unlock();
		}
	}

	public Object take() throws InterruptedException {
		lock.lock();
		try {
			while (count == 0)
				notEmpty.await();
			Object x = items[takeptr];
			if (++takeptr == items.length)
				takeptr = 0;
			--count;
			notFull.signal();
			return x;
		} finally {
			lock.unlock();
		}
	}
}

./CriticalSectionEx.java 3/7

[
top][prev][next]
/**
 * A motivating example for thread synchronization.
 * 
 */
public class CriticalSectionEx {

    public static void main(String[] args) {

        for (int i = 1; i <= 100; i++) {
            Integer[] x = { new Integer(0) };

            Thread tc = new IncThread(x);
            Thread td = new DecThread(x);

            tc.start();
            td.start();

            try {
                tc.join();
                td.join();
            }
            catch (InterruptedException e) {
            }

            System.out.print(x[0] + "\t");
            if (i % 10 == 0) {
                System.out.println();
            }
        }
    }
}

class IncThread extends Thread {

    private Integer[] x;

    public IncThread(Integer[] x) {
        this.x = x;
    }

    public void run() {
        for (int i = 0; i < 500000; i++) {
            x[0] = new Integer(x[0] + 1);
        }
    }
}

class DecThread extends Thread {

    private Integer[] x;

    public DecThread(Integer[] x) {
        this.x = x;
    }

    public void run() {
        for (int i = 0; i < 500000; i++) {
            x[0] = new Integer(x[0] - 1);
        }
    }
}

./index.html 4/7

[
top][prev][next]
<html>
<head><title>Examples for /home/faculty/sprenkle/public_html/cs330/examples/22-sync</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/22-sync</h1>
<ul>
<li><a href=".//code.html">All IN ONE FILE (pretty syntax)</a>
<li><a href=".//BoundedBuffer.java">BoundedBuffer.java</a></li>
<li><a href=".//CriticalSectionEx.java">CriticalSectionEx.java</a></li>
<li><a href=".//SerializationEx.java">SerializationEx.java</a></li>
<li><a href=".//SerializationExCond.java">SerializationExCond.java</a></li>
<li><a href=".//SerializationExSem.java">SerializationExSem.java</a></li>
</ul>
</body>

./SerializationExCond.java 5/7

[
top][prev][next]
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * A solution using condition variables
 * 
 * @author Grant Braught
 * @author CSCI330
 */
public class SerializationExCond {

	public static Lock myLock = new ReentrantLock();
	public static Condition notDone = myLock.newCondition();
	public static boolean done;

	public static void main(String[] args) {

		for (int i = 1; i <= 100; i++) {
			int[] x = { 1, 0 };

			Thread ta = new ThreadACond(x);
			Thread tb = new ThreadBCond(x);

			done = false;
			ta.start();
			tb.start();

			try {
				ta.join();
				tb.join();
			} catch (InterruptedException e) {
			}

			if (i % 10 == 0) {
				System.out.println();
			} else {
				System.out.print("\t");
			}
		}
	}
}

class ThreadACond extends Thread {

	private int[] x;

	public ThreadACond(int[] x) {
		this.x = x;
	}

	public void run() {
		doWork();
		SerializationExCond.myLock.lock();
		x[0] = 5;
		doWork();
		System.out.print(x[0] + " ");
		SerializationExCond.done = true;
		SerializationExCond.notDone.signal();
		SerializationExCond.myLock.unlock();
	}

	public void doWork() {
		for (int i = 0; i < 100000; i++) {
			new Double(Math.random());
		}
	}
}

class ThreadBCond extends Thread {

	private int[] x;

	public ThreadBCond(int[] x) {
		this.x = x;
	}

	public void run() {
		doWork();
		SerializationExCond.myLock.lock();
		while (!SerializationExCond.done) {
			try {
				SerializationExCond.notDone.await();
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}
		x[1] = x[0] + 3;
		doWork();
		System.out.print(x[1] + " ");
		SerializationExCond.myLock.unlock();
	}

	public void doWork() {
		for (int i = 0; i < 100000; i++) {
			new Double(Math.random());
		}
	}
}

./SerializationEx.java 6/7

[
top][prev][next]
/**
 * A motivating example for thread synchronization.
 * 
 * @author Grant Braught
 */
public class SerializationEx {

	public static void main(String[] args) {

		for (int i = 1; i <= 100; i++) {
			int[] x = { 1, 0 };

			Thread ta = new ThreadA(x);
			Thread tb = new ThreadB(x);

			ta.start();
			tb.start();

			try {
				ta.join();
				tb.join();
			} catch (InterruptedException e) {
			}

			if (i % 10 == 0) {
				System.out.println();
			} else {
				System.out.print("\t");
			}
		}
	}
}

class ThreadA extends Thread {

	private int[] x;

	public ThreadA(int[] x) {
		this.x = x;
	}

	public void run() {
		doWork();
		x[0] = 5;
		doWork();
		System.out.print(x[0] + " ");
	}

	public void doWork() {
		for (int i = 0; i < 100000; i++) {
			new Double(Math.random());
		}
	}
}

class ThreadB extends Thread {

	private int[] x;

	public ThreadB(int[] x) {
		this.x = x;
	}

	public void run() {
		doWork();
		x[1] = x[0] + 3;
		doWork();
		System.out.print(x[1] + " ");
	}

	public void doWork() {
		for (int i = 0; i < 100000; i++) {
			new Double(Math.random());
		}
	}
}

./SerializationExSem.java 7/7

[
top][prev][next]
import java.util.concurrent.*;

/**
 * A solution using semaphores
 * 
 * @author Grant Braught
 * @author CSCI330
 */
public class SerializationExSem {

	public static Semaphore aSem = new Semaphore(1);
	public static Semaphore bSem = new Semaphore(0);

	public static void main(String[] args) {

		for (int i = 1; i <= 100; i++) {
			int[] x = { 1, 0 };

			Thread ta = new ThreadASem(x);
			Thread tb = new ThreadBSem(x);

			ta.start();
			tb.start();

			try {
				ta.join();
				tb.join();
			} catch (InterruptedException e) {
			}

			if (i % 10 == 0) {
				System.out.println();
			} else {
				System.out.print("\t");
			}
		}
	}
}

class ThreadASem extends Thread {

	private int[] x;

	public ThreadASem(int[] x) {
		this.x = x;
	}

	public void run() {
		doWork();
		try {
			SerializationExSem.aSem.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		x[0] = 5;
		doWork();
		System.out.print(x[0] + " ");
		SerializationExSem.bSem.release();
	}

	public void doWork() {
		for (int i = 0; i < 100000; i++) {
			new Double(Math.random());
		}
	}
}

class ThreadBSem extends Thread {

	private int[] x;

	public ThreadBSem(int[] x) {
		this.x = x;
	}

	public void run() {
		doWork();
		try {
			SerializationExSem.bSem.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		x[1] = x[0] + 3;
		doWork();
		System.out.print(x[1] + " ");
		SerializationExSem.aSem.release();
	}

	public void doWork() {
		for (int i = 0; i < 100000; i++) {
			new Double(Math.random());
		}
	}
}

Generated by GNU Enscript 1.6.6.