Skip to content

Instantly share code, notes, and snippets.

View aerodame's full-sized avatar

Steve Dame aerodame

View GitHub Profile
@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];
@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());
@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();
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();
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
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);
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();
public void suspendTask() {
synchronized (this) {
paused = true;
}
}
public void resumeTask() {
synchronized (this) {
paused = false;
notify();
/**
* 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);
}
}
@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) {