Last active
October 14, 2015 07:00
-
-
Save VincentTatan/7fdc87b427be4973f4a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Thread; | |
import java.util.Random; | |
import java.util.concurrent.locks.*; | |
/** | |
* Created by kevinsteppe on 2/10/15. | |
*/ | |
public class ThreadDemoExc { | |
public static final int numLoops = 8; | |
private static int num_threads = 4; | |
private static Lock lock = new ReentrantLock() ; | |
public static void main(String[] args) throws Exception { | |
Thread t1 = new Thread(new ThreadDemo(0,lock)); | |
Thread t2 = new Thread(new ThreadDemo(1,lock)); | |
t1.start(); | |
t2.start(); | |
t1.join(); | |
t2.join(); //wait for both | |
System.out.println("===== end loop ====="); | |
} | |
} | |
class ThreadDemo implements Runnable { | |
private static Lock lock ; | |
int id; | |
Random random = new Random(); | |
public ThreadDemo(int i,Lock lock) { | |
id = i; | |
this.lock=lock; | |
} | |
public void run() { | |
try { | |
lock.lock(); | |
for (int i = 0; i < 10; i++) { | |
System.out.println("Hello World "+id); | |
Thread.yield(); | |
int x = random.nextInt(4); | |
if (x == id) { | |
lock.unlock(); | |
Thread.sleep(200); | |
lock.lock(); | |
throw new Exception("An exception! "+id); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
lock.unlock(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run: | |
Hello World | |
Hello World | |
Hello World | |
Hello World | |
Hello World | |
Hello World | |
Hello World | |
Hello World | |
Hello World | |
===== end loop ===== | |
java.lang.Exception: An exception! | |
at Thread.ThreadDemo.run(ThreadDemoExc.java:49) | |
at java.lang.Thread.run(Thread.java:724) | |
Exception in thread "Thread-1" java.lang.Exception: An exception! | |
at Thread.ThreadDemo.run(ThreadDemoExc.java:49) | |
at java.lang.Thread.run(Thread.java:724) | |
java.lang.IllegalMonitorStateException | |
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:155) | |
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260) | |
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:460) | |
at Thread.ThreadDemo.run(ThreadDemoExc.java:55) | |
at java.lang.Thread.run(Thread.java:724) | |
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException | |
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:155) | |
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260) | |
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:460) | |
at Thread.ThreadDemo.run(ThreadDemoExc.java:55) | |
at java.lang.Thread.run(Thread.java:724) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment