• Log in or sign up for an account here: https://www.sythe.org/login

Random Counting Muliti-Threaded Program

SuF

Legend

Pirate
Retired Global Moderator
Joined
Jan 21, 2007
Sythe Gold
1,234
Messages
14,211
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
I made this to get some practice with threads so I could understand how to use them, and how to make them interact. I used my textbook example to work off of, and got the counting idea from there. I just copied and modified slightly some code from the book, which I do not fully understand yet, but my main focus was not about those things(The enhanced for loop, and the ArrayList thing).

This program counts the amount of time since the start of the program(in seconds). It displays what time it is at, at certain predetermined times(writeTime thread handles that). It also displays the current time every minute, and when enter anything.

Heres the program. I learned a lot making it, so I hope you can learn by studying it. Cheers!

Code:
package threads;

import java.util.*;

public class Threads
{
	public static void main(String arg[])
	{
		Clock Clock = new Clock();
		getTime a = new getTime(Clock);
		writeMin b = new writeMin(Clock);

		ArrayList<Runnable> times = new ArrayList<Runnable>();

		times.add(new writeTime(12, Clock));
		times.add(new writeTime(64, Clock));
		times.add(new writeTime(62, Clock));
		times.add(new writeTime(76, Clock));

		Clock.start();
		a.start();
		b.start();

		for (Runnable e : times)
			new Thread(e).start();
	}
}

interface TimeKeeper
{
	int getTime();
}

class Clock extends Thread implements TimeKeeper
{
	int t;

	public Clock()
	{

	}

	public void run()
	{
		while (true)
		{
			try
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException e)
			{
			}
			t++;
		}
	}

	public int getTime()
	{
		return t;
	}
}

class writeTime implements Runnable
{
	int writeTime;
	int minkeeper = 60;
	TimeKeeper keeper;

	public writeTime(int time, TimeKeeper keeper)
	{
		this.writeTime = time;
		this.keeper = keeper;
	}

	public void run()
	{
		boolean done = false;
		while (!done)
		{
			try
			{
				Thread.sleep(10);
			}
			catch (InterruptedException e)
			{
			}
			if (keeper.getTime() == writeTime)
			{
				System.out.println("Current Time: " + keeper.getTime());
				done = true;
			}
		}
	}
}

class getTime extends Thread
{
	TimeKeeper keeper;

	public getTime(TimeKeeper keeper)
	{
		this.keeper = keeper;
	}

	public void run()
	{
		Scanner a = new Scanner(System.in);
		while (true)
		{
			a.nextLine();
			System.out.println("Current Time: " + keeper.getTime());
		}
	}
}

class writeMin extends Thread
{
	TimeKeeper keeper;
	int minKeeper;

	public writeMin(TimeKeeper keeper)
	{
		this.keeper = keeper;
		minKeeper = 60;
	}

	public void run()
	{
		while (true)
		{
			if (keeper.getTime() == minKeeper)
			{
				minKeeper += 60;
				System.out.println("Current Time: " + keeper.getTime());
			}
		}
	}
}
 
Runescape Gamblingrunescape gambling
Back
Top