1 /* 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 @test 26 @summary Some tests of the Volatile Language Spec 27 @summary com.apple.junit.java.lang.LanguageSpec; 28 @run junit VolatileTest 29 */ 30 31 import junit.framework.*; 32 33 import java.util.Timer; 34 import java.util.TimerTask; 35 36 public class VolatileTest extends TestCase { 37 Thread t1; 38 Thread t2; 39 TimerTask stopper; 40 boolean done = false; 41 boolean failed= false; 42 volatile long sharedLong; 43 Timer timer; 44 45 class Stopper extends TimerTask{ 46 47 public void run(){ 48 done = true; 49 } 50 } 51 52 public void setUp(){ 53 //create two threads which change the value of sharedLong 54 //If sharedLong not declared as volatile, then VM splits the 55 //long into two 32 bit sections, and performs operations on them 56 //seperately. The volatile decleration ensures that the actions 57 //performed on sharedLong occur in the same order dictated by the thread, 58 //regardless of whether the long was split into two sections or not. 59 60 t1 = new Thread( new Runnable() { 61 public void run() { 62 while(!done && !failed){ 63 sharedLong = Long.MAX_VALUE; 64 long temp = sharedLong; 65 //depending on the order of thread execution, if 66 //sharedLong is declared as volatile, temp can assume 67 //only one of the the two valid values below, and cannot 68 //have an intermediate state. 69 70 if ((temp != 0L) && (temp != Long.MAX_VALUE)) { 71 System.out.println( "t1" + temp); 72 failed = true; 73 } 74 } 75 } 76 } ); 77 78 t2 = new Thread( new Runnable() { 79 public void run() { 80 while(!done && !failed){ 81 sharedLong = 0L; 82 long temp = sharedLong; 83 //depending on the order of thread execution, if 84 //sharedLong is declared as volatile, temp can assume 85 //only one of the the two valid values below, and cannot 86 //have an intermediate state. 87 if ((temp != 0L) && (temp != Long.MAX_VALUE)) { 88 System.out.println( "t2" + temp); 89 failed = true; 90 } 91 } 92 } 93 } ); 94 } 95 96 public void testThread() throws Exception{ 97 timer = new Timer(); 98 //check version, if less than 1.5.0, then ignore test. Change this in 99 //leopard 100 String version = System.getProperty("java.vm.version").substring(0,3); 101 if(Double.parseDouble(version) < 1.5){ 102 assertTrue(true); 103 timer.cancel(); 104 } 105 else{ 106 try{ 107 timer.schedule(new Stopper(), 2000); 108 t1.start(); 109 t2.start(); 110 t1.join(); 111 t2.join(); 112 //check that the volatile declaration of sharedLong 113 //prevented intermediate states from appearing. 114 assertFalse("volatile long got clobbered", failed); 115 } 116 finally{ 117 timer.cancel(); 118 } 119 120 } 121 } 122 123 public static Test suite() { 124 return new TestSuite(VolatileTest.class); 125 } 126 127 public static void main (String[] args) throws RuntimeException { 128 TestResult tr = junit.textui.TestRunner.run(suite()); 129 if((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 130 throw new RuntimeException("### Unexpected JUnit errors or failures."); 131 } 132 } 133 }