1 import java.util.Vector;
3 public class LockRecords {
4 static final int N = 3;
13 protected void finalize() throws Throwable
15 System.out.println("\tObject #" + id + " gets finalized");
16 throw new Exception("Object #" + id + " is nasty!");
20 static class MyThread extends Thread
23 public synchronized void run()
25 System.out.println("\t" + name + ": Starting ...");
27 for (int i=0; i<N; i++) {
28 MyObject o = (MyObject) v.get(i);
29 System.out.println("\t" + name + ": Trying on #" + o.id);
31 System.out.println("\t" + name + ": Locked on #" + o.id);
33 System.out.println("\t" + name + ": Releasing #" + o.id);
36 while (o.counter > 0) {
40 } catch (Exception e) {
43 System.out.println("\t" + name + ": finished.");
47 public synchronized void test()
49 System.out.println("Creating Objects ...");
51 for (int i=0; i<N; i++) {
52 MyObject o = new MyObject();
58 System.out.println("Starting Blocking Threads A + B...");
59 MyThread threadA = new MyThread();
60 MyThread threadB = new MyThread();
61 threadA.name = "A"; threadA.start();
62 threadB.name = "B"; threadB.start();
66 } catch (Exception e) {
70 System.out.println("Cleaning up ...");
74 System.out.println("Waiting some seconds ...");
77 } catch (Exception e) {
81 System.out.println("Cleaning up again ...");
85 public static void main(String args[])
87 LockRecords test = new LockRecords();