2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.test.java.concurrent;
20 import gov.nasa.jpf.util.test.TestJPF;
22 import java.util.concurrent.Semaphore;
24 import org.junit.Test;
27 * simple test for Java 1.5 java.util.concurrent support
29 public class SemaphoreTest extends TestJPF {
33 static final int MAX = 1;
34 static final Semaphore avail = new Semaphore(MAX, true);
35 static Resource[] items = new Resource[MAX];
36 static boolean[] isUsed = new boolean[MAX];
37 static final Object lock = new Object();
41 for (int i = 0; i < items.length; i++) {
42 items[i] = new Resource(i);
46 static class Resource {
52 this.id = "Resource-" + id;
55 public void use(String newUser) {
56 assert user == null : "resource " + id + " in use by " + user +
57 ", but attempted to be acquired by: " + newUser;
61 public void release() {
66 public String toString() {
71 public static Resource getItem() throws InterruptedException {
75 for (int i = 0; i < MAX; i++) {
82 assert false : "couldn't find unused resource";
86 public static void putItem(Resource o) {
88 for (int i = 0; i < MAX; i++) {
100 static class Client implements Runnable {
104 String id = Thread.currentThread().getName();
107 System.out.println(id + " acquiring resource..");
108 Resource r = SemaphoreTest.getItem();
109 System.out.println(id + " got resource: " + r);
115 System.out.println(id + " releasing resource: " + r);
116 SemaphoreTest.putItem(r);
117 System.out.println(id + " released");
119 } catch (InterruptedException ix) {
120 System.out.println("!! INTERRUPTED");
125 //--------------- the test cases
127 public void testResourceAcquisition() {
128 if (verifyNoPropertyViolation()) {
129 for (int i = 0; i <= MAX; i++) {
130 Thread t = new Thread(new Client());