KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > SetSemaphore


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test;
22
23 import com.db4o.ext.*;
24 import com.db4o.foundation.*;
25
26 public class SetSemaphore {
27
28     public void test() {
29
30         final ExtObjectContainer client1 = Test.objectContainer();
31
32         Test.ensure(client1.setSemaphore("hi", 0));
33         Test.ensure(client1.setSemaphore("hi", 0));
34
35         if (Test.clientServer) {
36             final ExtObjectContainer client2 = Test.open();
37             final ExtObjectContainer client3 = Test.open();
38             final ExtObjectContainer client4 = Test.open();
39             final ExtObjectContainer client5 = Test.open();
40
41             Test.ensure(!client2.setSemaphore("hi", 0));
42             client1.releaseSemaphore("hi");
43             Test.ensure(client2.setSemaphore("hi", 50));
44             Test.ensure(!client1.setSemaphore("hi", 0));
45             Test.ensure(!client3.setSemaphore("hi", 0));
46             
47             new GetAndRelease(client3);
48             new GetAndRelease(client2);
49             new GetAndRelease(client1);
50             new GetAndRelease(client4);
51             new GetAndRelease(client5);
52             
53             Cool.sleepIgnoringInterruption(1000);
54             Test.ensure(client1.setSemaphore("hi", 0));
55             client1.close();
56             
57             new GetAndRelease(client3);
58             new GetAndRelease(client2);
59             Cool.sleepIgnoringInterruption(1000);
60             
61             client2.close();
62             client3.close(); // the last one opened remains
63
client4.close(); // open for other tests
64

65             client5.setSemaphore("hi", 1000);
66         }
67
68     }
69
70     static class GetAndRelease implements Runnable JavaDoc {
71
72         ExtObjectContainer client;
73
74         public GetAndRelease(ExtObjectContainer client) {
75             this.client = client;
76             new Thread JavaDoc(this).start();
77         }
78
79         public void run() {
80             long time = System.currentTimeMillis();
81             Test.ensure(client.setSemaphore("hi", 50000));
82             time = System.currentTimeMillis() - time;
83             // System.out.println("Time to get semaphore: " + time);
84
Cool.sleepIgnoringInterruption(50);
85
86             // System.out.println("About to release semaphore.");
87
client.releaseSemaphore("hi");
88         }
89     }
90
91 }
92
Popular Tags