KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > deadlocks > DeadlockTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-2001 by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8

9 package test.deadlocks;
10
11 import org.ozoneDB.Database;
12 import org.ozoneDB.RemoteDatabase;
13 import org.ozoneDB.ExternalDatabase;
14 import test.simple.Garage;
15 import test.simple.GarageImpl;
16 import test.OzoneTestCase;
17
18
19 /**
20  */

21 class AccessThread extends Thread JavaDoc {
22     RemoteDatabase db;
23     Garage g1;
24     Garage g2;
25
26
27     /** */
28     public AccessThread(RemoteDatabase _db, String JavaDoc _g1, String JavaDoc _g2) throws Exception JavaDoc {
29         System.out.println("thread: g1 = " + _g1);
30         db = _db;
31         g1 = (Garage) db.objectForName(_g1);
32         g2 = (Garage) db.objectForName(_g2);
33     }
34
35
36     /** */
37     public void run() {
38         try {
39             System.out.println(g1.toString());
40             g1._langeTA(g2);
41             g1.printAll();
42         } catch (Exception JavaDoc e) {
43             e.printStackTrace();
44         }
45     }
46 }
47
48
49 /**
50  */

51 public class DeadlockTest extends OzoneTestCase {
52
53     public DeadlockTest(String JavaDoc name) {
54         super(name);
55     }
56
57
58     public void testDeadlock() {
59         try {
60             // 1st connection to remote database
61
if (db() instanceof RemoteDatabase) {
62
63             } else {
64                 System.out.println("testDeadlock only works on remote databases");
65                 return;
66             }
67             RemoteDatabase db = (RemoteDatabase) db();
68
69             // 2nd connection to remote database
70
RemoteDatabase db2 = (RemoteDatabase) ExternalDatabase.openDatabase(getDbUrl());
71             System.out.println("connected (remote)...");
72
73             Garage garage1 = (Garage) db.objectForName("g1");
74             Garage garage2 = (Garage) db.objectForName("g2");
75             if (garage1 == null) {
76                 System.out.println("neue objekte erzeugen");
77                 garage1 = (Garage) db.createObject(GarageImpl.class.getName(), Database.Private, "g1");
78                 garage2 = (Garage) db.createObject(GarageImpl.class.getName(), Database.Private, "g2");
79             }
80
81             Thread JavaDoc t1 = new AccessThread(db, "g1", "g2");
82             t1.setPriority(Thread.currentThread().getPriority());
83             t1.setName("t1");
84             t1.start();
85
86             Thread.currentThread().sleep(2000);
87
88             Thread JavaDoc t2 = new AccessThread(db2, "g2", "g1");
89             t2.setPriority(Thread.currentThread().getPriority());
90             t2.setName("t2");
91             t2.start();
92
93             Thread.currentThread().sleep(1000);
94             while (t1.isAlive() || t2.isAlive()) {
95                 Thread.currentThread().sleep(100);
96             }
97             db2.close();
98             System.out.println("deconnected...");
99         } catch (Exception JavaDoc e) {
100             fail(e.toString());
101         }
102     }
103 }
104
Popular Tags