Skip to content

Commit

Permalink
Added via the Open Science Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
jh27539 authored and Josh Staging Hernandez committed Dec 4, 2024
1 parent 1b01649 commit 8c8148d
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author evivehealth on 08/02/19.
*/
// Java program depicting
// concurrent programming in action.

// Runnable Class that defines the logic
// of run method of runnable interface
public class Counter implements Runnable
{
private final MainApp mainApp;
private final int loopLimit;
private final String task;

// Constructor to get a reference to the main class
public Counter
(MainApp mainApp, int loopLimit, String task)
{
this.mainApp = mainApp;
this.loopLimit = loopLimit;
this.task = task;
}

// Prints the thread name, task number and
// the value of counter
// Calls pause method to allow multithreading to occur
@Override
public void run()
{
for (int i = 0; i < loopLimit; i++)
{
System.out.println("Thread: " +
Thread.currentThread().getName() + " Counter: "
+ (i + 1) + " Task: " + task);
mainApp.pause(Math.random());
}
}
}
class MainApp
{

// Starts the threads. Pool size 2 means at any time
// there can only be two simultaneous threads
public void startThread()
{
ExecutorService taskList =
Executors.newFixedThreadPool(2);
for (int i = 0; i < 5; i++)
{
// Makes tasks available for execution.
// At the appropriate time, calls run
// method of runnable interface
taskList.execute(new Counter(this, i + 1,
"task " + (i + 1)));
}

// Shuts the thread that's watching to see if
// you have added new tasks.
taskList.shutdown();
}

// Pauses execution for a moment
// so that system switches back and forth
public void pause(double seconds)
{
try
{
Thread.sleep(Math.round(1000.0 * seconds));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

// Driver method
public static void main(String[] args)
{
new MainApp().startThread();
}
}

0 comments on commit 8c8148d

Please sign in to comment.