diff --git a/src/main/java/com/jdk/jdk/thread/CountDownLatchDemo.java b/src/main/java/com/demo/thread/CountDownLatchDemo.java similarity index 99% rename from src/main/java/com/jdk/jdk/thread/CountDownLatchDemo.java rename to src/main/java/com/demo/thread/CountDownLatchDemo.java index cfaaf92..f133bed 100644 --- a/src/main/java/com/jdk/jdk/thread/CountDownLatchDemo.java +++ b/src/main/java/com/demo/thread/CountDownLatchDemo.java @@ -1,4 +1,4 @@ -package com.jdk.jdk.thread; +package com.demo.thread; import java.util.concurrent.CountDownLatch; import java.util.logging.Level; diff --git a/src/main/java/com/jdk/jdk/thread/CyclicBarrierExample.java b/src/main/java/com/demo/thread/CyclicBarrierExample.java similarity index 98% rename from src/main/java/com/jdk/jdk/thread/CyclicBarrierExample.java rename to src/main/java/com/demo/thread/CyclicBarrierExample.java index ee0c777..61c88db 100644 --- a/src/main/java/com/jdk/jdk/thread/CyclicBarrierExample.java +++ b/src/main/java/com/demo/thread/CyclicBarrierExample.java @@ -1,4 +1,4 @@ -package com.jdk.jdk.thread; +package com.demo.thread; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; diff --git a/src/main/java/com/demo/thread/InterruptAndContinueDemo.java b/src/main/java/com/demo/thread/InterruptAndContinueDemo.java new file mode 100644 index 0000000..8a8a078 --- /dev/null +++ b/src/main/java/com/demo/thread/InterruptAndContinueDemo.java @@ -0,0 +1,81 @@ +package com.demo.thread; + +public class InterruptAndContinueDemo { + + private static final Object sharedResource = new Object(); // Shared resource + private static boolean isNewTaskDone = false; // Flag to signal completion of newTaskThread + + public static void main(String[] args) throws InterruptedException { + + // Create a sub-thread for the long-running task + Thread subThread = new Thread(() -> { + synchronized (sharedResource) { + System.out.println("SubThread: Starting a long-running task..."); + try { + System.out.println("SubThread: Acquired the resource, working..."); + // Simulate long-running task + Thread.sleep(5000); // Mock long-running task + } catch (InterruptedException e) { + System.out.println("SubThread: Interrupted by newTask! Now I waiting for newTask to finish..."); + + // Wait until newTaskThread finishes its work + while (!isNewTaskDone) { + try { + sharedResource.wait(); // SubThread waits for notification + } catch (InterruptedException waitException) { + Thread.currentThread().interrupt(); // Restore interrupt flag + } + } + + // Continue subThread work after newTaskThread finishes + System.out.println("SubThread: NewTask finished, resuming work..."); + // Resume long-running work or remaining tasks + try { + Thread.sleep(3000); // Mock remaining work + } catch (InterruptedException finalException) { + Thread.currentThread().interrupt(); + } + System.out.println("SubThread: Finished its work."); + } + } + }); + + // Start the sub-thread + subThread.start(); + + // Simulate some delay before the newTask thread interrupts + Thread.sleep(2000); // Wait for 2 seconds to simulate an urgent request + + // Create the newTask thread which needs the resource urgently + Thread newTaskThread = new Thread(() -> { + System.out.println("NewTask: Urgent task started, interrupting sub-thread..."); + + // Interrupt the sub-thread to take over the resource + subThread.interrupt(); + + // Acquire the shared resource and perform urgent work + synchronized (sharedResource) { + System.out.println("NewTask: Acquired the resource, performing urgent task..."); + try { + Thread.sleep(2000); // Mock urgent work + } catch (InterruptedException e) { + System.out.println("NewTask: Interrupted during urgent task."); + } + + // Notify that newTask has completed its work + System.out.println("NewTask: Urgent task completed, releasing resource..."); + isNewTaskDone = true; // Set the flag to true to signal completion + sharedResource.notify(); // Notify sub-thread that newTask is done + } + }); + + // Start the newTask thread + newTaskThread.start(); + + // Wait for both threads to finish + subThread.join(); + newTaskThread.join(); + + System.out.println("MainThread: All tasks finished."); + } +} diff --git a/src/main/java/com/demo/thread/InterruptDemo.java b/src/main/java/com/demo/thread/InterruptDemo.java new file mode 100644 index 0000000..01d9ff3 --- /dev/null +++ b/src/main/java/com/demo/thread/InterruptDemo.java @@ -0,0 +1,64 @@ +package com.demo.thread; + +public class InterruptDemo { + + private static Object sharedResource = new Object(); // Shared resource + + public static void main(String[] args) throws InterruptedException { + + // Create a sub-thread for the long-running task + Thread subThread = new Thread(() -> { + System.out.println("SubThread: Starting a long-running task..."); + + synchronized (sharedResource) { + try { + // Simulate long-running task + System.out.println("SubThread: Acquired the resource, working..."); + Thread.sleep(5000); // Mock long-running task + } catch (InterruptedException e) { + // Re-interrupting to signal that the thread was interrupted + System.out.println("SubThread: Interrupted! Releasing resource..."); + Thread.currentThread().interrupt(); + } + } + + // After being interrupted, stop the work + if (Thread.currentThread().isInterrupted()) { + System.out.println("SubThread: Stopping the long-running task."); + } + }); + + + // Simulate some delay before the newTask thread interrupts + Thread.sleep(2000); // Wait for 2 seconds to simulate urgent request + + // Create the newTask thread which needs the resource urgently + Thread newTaskThread = new Thread(() -> { + System.out.println("NewTask: This is an urgent task, now i am interrupting sub-thread..."); + subThread.interrupt(); + + // Try to acquire the resource after interrupting the sub-thread + synchronized (sharedResource) { + System.out.println("NewTask: Acquired the resource, proceeding with urgent task..."); + // Do some urgent work + try { + Thread.sleep(2000); // Mock urgent work + } catch (InterruptedException e) { + System.out.println("NewTask: Interrupted during urgent task."); + } + } + }); + + + // Start the sub-thread + subThread.start(); + // Start the newTask thread + newTaskThread.start(); + + // Wait for both threads to finish + subThread.join(); + newTaskThread.join(); + + System.out.println("MainThread: All tasks finished."); + } +} diff --git a/src/main/java/com/jdk/jdk/thread/ProducerConsumerPattern.java b/src/main/java/com/demo/thread/ProducerConsumerPattern.java similarity index 98% rename from src/main/java/com/jdk/jdk/thread/ProducerConsumerPattern.java rename to src/main/java/com/demo/thread/ProducerConsumerPattern.java index c6e3737..c954a9e 100644 --- a/src/main/java/com/jdk/jdk/thread/ProducerConsumerPattern.java +++ b/src/main/java/com/demo/thread/ProducerConsumerPattern.java @@ -1,4 +1,4 @@ -package com.jdk.jdk.thread; +package com.demo.thread; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; diff --git a/src/main/java/com/jdk/jdk/thread/RunnableDemo.java b/src/main/java/com/demo/thread/RunnableDemo.java similarity index 97% rename from src/main/java/com/jdk/jdk/thread/RunnableDemo.java rename to src/main/java/com/demo/thread/RunnableDemo.java index d12ec3a..6424a83 100644 --- a/src/main/java/com/jdk/jdk/thread/RunnableDemo.java +++ b/src/main/java/com/demo/thread/RunnableDemo.java @@ -1,4 +1,4 @@ -package com.jdk.jdk.thread; +package com.demo.thread; /** * Created by tbu on 6/26/2014. diff --git a/src/main/java/com/jdk/jdk/thread/SleepingThread.java b/src/main/java/com/demo/thread/SleepingThread.java similarity index 96% rename from src/main/java/com/jdk/jdk/thread/SleepingThread.java rename to src/main/java/com/demo/thread/SleepingThread.java index 920356c..97ae81e 100644 --- a/src/main/java/com/jdk/jdk/thread/SleepingThread.java +++ b/src/main/java/com/demo/thread/SleepingThread.java @@ -1,4 +1,4 @@ -package com.jdk.jdk.thread; +package com.demo.thread; /** * Created by tbu on 6/26/2014. diff --git a/src/main/java/com/jdk/jdk/thread/ThreadCountTest.java b/src/main/java/com/demo/thread/ThreadCountTest.java similarity index 98% rename from src/main/java/com/jdk/jdk/thread/ThreadCountTest.java rename to src/main/java/com/demo/thread/ThreadCountTest.java index 8720a84..8108f4b 100644 --- a/src/main/java/com/jdk/jdk/thread/ThreadCountTest.java +++ b/src/main/java/com/demo/thread/ThreadCountTest.java @@ -1,4 +1,4 @@ -package com.jdk.jdk.thread; +package com.demo.thread; public class ThreadCountTest { public static void main(String[] args) { diff --git a/src/main/java/com/jdk/jdk/thread/threadlocal/InheritableThreadLocalDemo.java b/src/main/java/com/demo/thread/threadlocal/InheritableThreadLocalDemo.java similarity index 95% rename from src/main/java/com/jdk/jdk/thread/threadlocal/InheritableThreadLocalDemo.java rename to src/main/java/com/demo/thread/threadlocal/InheritableThreadLocalDemo.java index f749807..0412b54 100644 --- a/src/main/java/com/jdk/jdk/thread/threadlocal/InheritableThreadLocalDemo.java +++ b/src/main/java/com/demo/thread/threadlocal/InheritableThreadLocalDemo.java @@ -1,4 +1,4 @@ -package com.jdk.jdk.thread.threadlocal; +package com.demo.thread.threadlocal; /** * diff --git a/src/main/java/com/jdk/jdk/thread/threadlocal/ThreadLocalDemo.java b/src/main/java/com/demo/thread/threadlocal/ThreadLocalDemo.java similarity index 96% rename from src/main/java/com/jdk/jdk/thread/threadlocal/ThreadLocalDemo.java rename to src/main/java/com/demo/thread/threadlocal/ThreadLocalDemo.java index cfb5183..04a0046 100644 --- a/src/main/java/com/jdk/jdk/thread/threadlocal/ThreadLocalDemo.java +++ b/src/main/java/com/demo/thread/threadlocal/ThreadLocalDemo.java @@ -1,4 +1,4 @@ -package com.jdk.jdk.thread.threadlocal; +package com.demo.thread.threadlocal; public class ThreadLocalDemo { public static void main(String[] args) diff --git a/src/main/java/com/jdk/jdk/thread/threadlocal/ThreadLocalDemoWithoutInitValue.java b/src/main/java/com/demo/thread/threadlocal/ThreadLocalDemoWithoutInitValue.java similarity index 95% rename from src/main/java/com/jdk/jdk/thread/threadlocal/ThreadLocalDemoWithoutInitValue.java rename to src/main/java/com/demo/thread/threadlocal/ThreadLocalDemoWithoutInitValue.java index b65ad78..d51cfd5 100644 --- a/src/main/java/com/jdk/jdk/thread/threadlocal/ThreadLocalDemoWithoutInitValue.java +++ b/src/main/java/com/demo/thread/threadlocal/ThreadLocalDemoWithoutInitValue.java @@ -1,4 +1,4 @@ -package com.jdk.jdk.thread.threadlocal; +package com.demo.thread.threadlocal; public class ThreadLocalDemoWithoutInitValue { public static void main(String[] args)