Random Counting Muliti-Threaded Program

Discussion in 'Programming General' started by SuF, Nov 30, 2008.

Random Counting Muliti-Threaded Program
  1. Unread #1 - Nov 30, 2008 at 9:37 PM
  2. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Random Counting Muliti-Threaded Program

    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());
    			}
    		}
    	}
    }
     
  3. Unread #2 - Dec 1, 2008 at 12:45 AM
  4. xxthebeastxx
    Joined:
    May 17, 2008
    Posts:
    1,680
    Referrals:
    1
    Sythe Gold:
    0

    xxthebeastxx Guru
    Banned

    Random Counting Muliti-Threaded Program

    Very organized, and textbook-ey. Good job.
     
< Windows Live Messenger - Desktop contact list | Help , making parser >

Users viewing this thread
1 guest


 
 
Adblock breaks this site