-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix-0000: quick update & refactoring
- Loading branch information
Showing
11 changed files
with
154 additions
and
9 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...om/jdk/jdk/thread/CountDownLatchDemo.java → ...a/com/demo/thread/CountDownLatchDemo.java
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
2 changes: 1 addition & 1 deletion
2
.../jdk/jdk/thread/CyclicBarrierExample.java → ...com/demo/thread/CyclicBarrierExample.java
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
81 changes: 81 additions & 0 deletions
81
src/main/java/com/demo/thread/InterruptAndContinueDemo.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...k/jdk/thread/ProducerConsumerPattern.java → .../demo/thread/ProducerConsumerPattern.java
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
2 changes: 1 addition & 1 deletion
2
...java/com/jdk/jdk/thread/RunnableDemo.java → ...in/java/com/demo/thread/RunnableDemo.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.jdk.jdk.thread; | ||
package com.demo.thread; | ||
|
||
/** | ||
* Created by tbu on 6/26/2014. | ||
|
2 changes: 1 addition & 1 deletion
2
...va/com/jdk/jdk/thread/SleepingThread.java → .../java/com/demo/thread/SleepingThread.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.jdk.jdk.thread; | ||
package com.demo.thread; | ||
|
||
/** | ||
* Created by tbu on 6/26/2014. | ||
|
2 changes: 1 addition & 1 deletion
2
...a/com/jdk/jdk/thread/ThreadCountTest.java → ...java/com/demo/thread/ThreadCountTest.java
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
2 changes: 1 addition & 1 deletion
2
...readlocal/InheritableThreadLocalDemo.java → ...readlocal/InheritableThreadLocalDemo.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.jdk.jdk.thread.threadlocal; | ||
package com.demo.thread.threadlocal; | ||
|
||
/** | ||
* | ||
|
2 changes: 1 addition & 1 deletion
2
...k/thread/threadlocal/ThreadLocalDemo.java → ...o/thread/threadlocal/ThreadLocalDemo.java
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
2 changes: 1 addition & 1 deletion
2
...ocal/ThreadLocalDemoWithoutInitValue.java → ...ocal/ThreadLocalDemoWithoutInitValue.java
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