Skip to content

Commit

Permalink
fix-0000: quick update & refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rossbu committed Oct 11, 2024
1 parent 310efbb commit f2686f5
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jdk.jdk.thread;
package com.demo.thread;

import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jdk.jdk.thread;
package com.demo.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/com/demo/thread/InterruptAndContinueDemo.java
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.");
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/demo/thread/InterruptDemo.java
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.");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jdk.jdk.thread;
package com.demo.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
Expand Down
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.
Expand Down
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jdk.jdk.thread;
package com.demo.thread;

public class ThreadCountTest {
public static void main(String[] args) {
Expand Down
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;

/**
*
Expand Down
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;

public class ThreadLocalDemo {
public static void main(String[] args)
Expand Down
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;

public class ThreadLocalDemoWithoutInitValue {
public static void main(String[] args)
Expand Down

0 comments on commit f2686f5

Please sign in to comment.