Example.java 1/4

[
top][prev][next]
/* In questo semplice programma abbiamo due thread:
 * - Calculator: esegue una serie di moltiplicazioni
 * - Writer: stampa a video una serie di numeri
 * Come si pu? verificare, ogni esecuzione produce un risultato diverso (non-determinismo)
 * perch? dipende dallo 
 */


class Calculator extends Thread {

	String nome;

	public Calculator(String nome) {
		this.nome = nome;
	}

	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println(nome + ": " + i);
			for (int j = 0; j < 1000000; j++) {
				int a = 100 * 100;
			}
		}
	}

}

class Writer implements Runnable {
	String nome;

	public Writer(String nome) {
		super();
		this.nome = nome;
	}

	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println(nome + ": " + i);
			for (int j = 0; j < 100; j++) {
				System.out.print(i);
			}
		}
	}

}

public class Example {
	public static void main(String[] args) {
		Calculator calculator = new Calculator("Calculator");
		Writer writer = new Writer("Writer");

		calculator.start();
		
		Thread threadWriter = new Thread(writer);
		threadWriter.start();

		try {
			// Il thread di main aspetta che il thread writer finisca prima di proseguire
			threadWriter.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println();
		System.out.println("Main: end");
	}
}

Mess.java 2/4

[
top][prev][next]
class Even {
	private int n = 0;

	public int next() {
		// POST?: next is always even
		++n;
		for (int i = 0; i < 1000000; i++) {
			int a = 100 * 100;
		}
		++n;
		return n;
	}

}


// Versione Sincronizzata (alternativamente si poteva anche rendere il
// metodo next synchronized)
class MyThread extends Thread {
	Even even;

	int id;

	public MyThread(Even even, int id) {
		this.even = even;
		this.id = id;
	}

	public void run() {
		synchronized (even) {
			for (int i = 0; i < 10; i++) {
				System.out.println(id + ": " + even.next());
			}
		}
	}

}

// Versione NON Sincronizzata
class MyThread2 extends Thread {
	Even even;

	int id;

	public MyThread2(Even even, int id) {
		this.even = even;
		this.id = id;
	}

	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println(id + ": " + even.next());
		}

	}

}

public class Mess {
	public static void main(String[] args) {
		Even e = new Even();

		for (int i = 0; i < 10; i++) {
			new MyThread(e, i).start();
		}
		for (int i = 0; i < 10; i++) {
			new MyThread2(e, i).start();
		}

	}

}

Deadlock.java 3/4

[
top][prev][next]
class A {
	synchronized public void ma1(B b) {
		for (int i = 0; i < 10000000; i++) {
			int x = 100 * 100;
		}
		System.out.println("Ciclo di ma1 finito");
		b.mb2();
	}

	synchronized public void ma2() {
		System.out.println("Metodo ma2 eseguito");
	}
}

class B {
	synchronized public void mb1(A a) {
		for (int i = 0; i < 10000000; i++) {
			int x = 100 * 100;
		}
		System.out.println("Ciclo di mb1 finito");
		a.ma2();
	}

	synchronized public void mb2() {
		System.out.println("Metodo mb2 eseguito");
	}
}

class C {
	A a;

	B b;

	public C(A a, B b) {
		super();
		// TODO Auto-generated constructor stub
		this.a = a;
		this.b = b;
	}

	synchronized public void ma1() {
		a.ma1(b);
	}

	synchronized public void mb1() {
		b.mb1(a);
	}
}

public class Deadlock {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final A a = new A();
		final B b = new B();
		
		final C c = new C(a,b);

		new Thread(new Runnable() {
			public void run() {
				c.ma1();
			}
		}).start();

		new Thread(new Runnable() {
			public void run() {
				c.mb1();
			}
		}).start();

		System.out.println("Main termina");

	}

}

Synch.java 4/4

[
top][prev][next]
package iwbank;

import java.util.*;

class Synch {
	synchronized int locking(int a, int b) {
		return a + b;
	}

	int not_locking(int a, int b) {
		return a + b;
	}

	private static final int ITERATIONS = 1000000000;

	static public void main(String[] args) {
		Synch tester = new Synch();

		double start = new Date().getTime();
		for (long i = ITERATIONS; --i >= 0;)
			tester.locking(0, 0);
		double end = new Date().getTime();
		double locking_time = end - start;

		start = new Date().getTime();
		for (long i = ITERATIONS; --i >= 0;)
			tester.not_locking(0, 0);
		end = new Date().getTime();
		double not_locking_time = end - start;

		double time_in_synchronization = locking_time - not_locking_time;

		System.out.println("Time lost to synchronization (millis.): "
				+ time_in_synchronization);

		System.out.println("Locking overhead per call: "
				+ (time_in_synchronization / ITERATIONS));

		System.out.println(not_locking_time / locking_time * 100.0
				+ "% increase");
	}
}

Generated by GNU enscript 1.6.4.