This file contains hidden or 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
| @Test | |
| @Timeout(value = 10, unit = TimeUnit.SECONDS) // Set a reasonable timeout | |
| public void testMultipleConcurrentTaskBurstTimes() throws InterruptedException { | |
| long[] bursts = { 500L, 1000L, 2000L, 4000L }; | |
| long allowedVariance = 50L; // Allow some variance for Task scheduling | |
| OSTask[] tasks = new OSTask[bursts.length]; | |
| long[] startTimes = new long[bursts.length]; | |
| long[] actualBursts = new long[bursts.length]; |
This file contains hidden or 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
| @Test | |
| public void testBurstSuspendResume() throws InterruptedException { | |
| final long quantum = 2000L; // 2sec quantum | |
| OSTask burstTask = new OSTask("Burst Task", 10000L); | |
| // start but immediately put into suspend state | |
| System.out.println("Burst Task Started: Burst=" + burstTask.getRemainingBurst()); | |
| burstTask.start(); | |
| burstTask.suspendTask(); | |
| System.out.println("Burst Task Suspended: Burst=" + burstTask.getRemainingBurst()); |
This file contains hidden or 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
| @Test | |
| @Timeout(value = 60, unit = TimeUnit.SECONDS) // Set a reasonable timeout | |
| public void testPingPongSuspendResume() throws InterruptedException { | |
| final long quantum = 2000L; // 2sec quantum | |
| OSTask pingTask = new OSTask("Ping Task", 10000L); | |
| OSTask pongTask = new OSTask("Pong Task", 10000L); | |
| // start but immediately put into suspend state | |
| pingTask.start(); | |
| pingTask.suspendTask(); |
This file contains hidden or 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
| public class OSTaskTest { | |
| final int default_sleep_time = 10; | |
| final long default_join_wait_time = 1000L; | |
| final long default_burst_duration = 1000L; | |
| /* ... other tests */ | |
| @Test | |
| public void testSuspendResume() throws InterruptedException { | |
| OSTask myTask = new OSTask("Test Task", default_burst_duration); | |
| myTask.start(); |
This file contains hidden or 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
| public class OSTaskTest { | |
| final int default_sleep_time = 10; | |
| final long default_join_wait_time = 1000L; | |
| final long default_burst_duration = 1000L; | |
| /* ... other tests */ | |
| @Test | |
| public void testIsTaskAliveBeforeStart() { | |
| OSTask myTask = new OSTask("Test Task", default_burst_duration); | |
| assertFalse(myTask.isTaskAlive()); // Should initially not be alive |
This file contains hidden or 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
| public class OSTaskTest { | |
| final int default_sleep_time = 10; | |
| final long default_join_wait_time = 1000L; | |
| final long default_burst_duration = 1000L; | |
| /* ... other tests */ | |
| @Test | |
| public void testStartStop() throws InterruptedException { | |
| OSTask myTask = new OSTask("Test Task", default_burst_duration); |
This file contains hidden or 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
| public class OSTaskTest { | |
| final int default_sleep_time = 10; | |
| final long default_join_wait_time = 1000L; | |
| final long default_burst_duration = 1000L; | |
| @Test | |
| public void testConstructorAndGetName() { | |
| String expectedName = "My Test Task"; | |
| OSTask myTask = new OSTask(expectedName, default_burst_duration); | |
| String actualName = myTask.getName(); |
This file contains hidden or 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
| public void suspendTask() { | |
| synchronized (this) { | |
| paused = true; | |
| } | |
| } | |
| public void resumeTask() { | |
| synchronized (this) { | |
| paused = false; | |
| notify(); |
This file contains hidden or 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
| /** | |
| * Simulate some CPU work (approx 4ms) | |
| */ | |
| private void doWork() { | |
| double value = 1.0; | |
| for (int j = 0; j < 10000; j++) { // Inner loop for more work | |
| value = Math.sqrt(value + j); | |
| } | |
| } |
This file contains hidden or 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
| @Override | |
| public void run() { | |
| long startTime = System.currentTimeMillis(); | |
| running = true; | |
| try { | |
| // Main work loop | |
| while (!Thread.interrupted() && running) { | |
| // Pause Logic | |
| synchronized (this) { | |
| while (paused && !Thread.interrupted() && running) { |