Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: working with multiple threads

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default working with multiple threads

    I'm trying to build a bank simulation using a queue, I got it to run but the results are not expected.
    i'm trying to have clients adding to a queue and Tellers serving them running for 2 minutes. After Client 2, for some reason the tellers are not running anymore. I know it's still running in the background but not doing anything. Can someone points me the problem. Thanks.
    HEre is the code

    import java.util.*;
    import java.io.*;
    import java.util.concurrent.*;
     
    //now with Tellers
     
    class banker3 implements Runnable
    {
    	public BlockingQueue<Client> bq = new LinkedBlockingQueue<Client>();
    	public static boolean stop = false;
    	private int arrival;
    	private static Random rand = new Random();
    	public banker3(BlockingQueue<Client> inq)
    	{
    		bq = inq;
    	}
     
    	public void run()
    	{
     
    		while(true)
    		try{
    		Client c = new Client();
    		bq.add(c);
    		arrival = rand.nextInt(5)+2;
    		System.out.printf("Adding Client %s to the queue\n", c.cID);
    		Thread.sleep(arrival*1000);
    		if(stop)
    		{
    			System.out.printf("There are %d clients totaled.\n", Client.getTotal());
    			int remain = 0;
    			while(bq.peek() != null){
    				Client temp = bq.remove();
    				remain++;
    			}
    			System.out.printf("There are %d clients remained in queue\n",remain);
    			break;
    		}
    		}
    		catch(InterruptedException e)
    		{
    			System.out.printf("Error\n",e);
    		}	
     
    	}
     
     
     
    	public static void main(String[] args)
    	{
    		BlockingQueue<Client>bankq = new LinkedBlockingQueue<Client>();
    		banker3 bank = new banker3(bankq);
    		Thread queing = new Thread(bank);
    		queing.setPriority(2);
    		Teller t1 = new Teller(bankq,1);
    		Teller t2 = new Teller(bankq,2);
    		Teller t3 = new Teller(bankq,3);
    		Teller t4 = new Teller(bankq,4);
    		Teller t5 = new Teller(bankq,5);
    		Thread tt1 = new Thread(t1);
    		tt1.setPriority(2);
    		Thread tt2 = new Thread(t2);
    		tt2.setPriority(2);
    		Thread tt3 = new Thread(t3);
    		tt3.setPriority(2);
    		Thread tt4 = new Thread(t4);
    		tt4.setPriority(2);
    		Thread tt5 = new Thread(t5);
    		tt5.setPriority(2);
    		StopWatch s = new StopWatch();
    		s.start();//start the timer
    		queing.start();
    		tt1.start();
    		tt2.start();
    		tt3.start();
    		tt4.start();
    		tt5.start();
    		while(s.isRunning())
    		{
    			if(s.elapSec() >= 120)
    			{
    				s.stop();
    				banker3.stop = true;
    				Teller.stop = true;
    				//System.out.printf("There are %d total clients\n", Client.getTotal());
    			}
     
    		}
     
     
    	}
     
     
     
    }
    //Client class
    class Client{
    private static int total = 0;
    public int waitTime;
    int cID;
    int allocatedTo;
    private static Random rand = new Random();
    public Client()
    {
    	waitTime = rand.nextInt(4)+2;
    	cID = ++total;
    	allocatedTo = rand.nextInt(6);
    }
    public static int getTotal()
    {
    	return total;
    }
     
     
    public static void setTotal()
    {
    	total--;
    }
    }
    //Teller class
    class Teller implements Runnable
    {
    	public BlockingQueue<Client>tq;
    	private int totaltime;
    	private int clientserved;
    	public static boolean stop = false;
    	public static boolean avail;
    	private static int tot = 0;
    	public static int servetime =0;
    	int tID;
    	public Teller(BlockingQueue<Client>iq, int id)
    	{
    		tq = iq;
    		tID = id;
    		avail = true;
    		clientserved =0;
    		totaltime=0;
     
    	}
     
    	public void run()
    	{
     
    		while(true)
    		try
    		{
    			serve();
    			Thread.sleep(servetime);
    			if(stop)
    			{
    				System.out.printf("Teller %d served %d clients and spent a total of %d secs\n", tID,clientserved,totaltime);
    				break;
    			}
    		}
    		catch(InterruptedException e)
    		{
    			System.out.printf("%s", e);
    		}	
    	}
     
    	public void serve()
    	{
    		Client temp = tq.peek();
    		if(temp != null && temp.allocatedTo == tID)
    		{
    			Client c = tq.remove();
    			servetime = c.waitTime;
    			clientserved++;
    			totaltime +=servetime;
    			System.out.printf("Teller %d is serving client %d\n", tID, c.cID); 
    		}
     
    	}
     
    }
     
     
    //stopwatch class
    class StopWatch {
        private long startTime = 0;
        private long stopTime = 0;
        private boolean running = false;
     
     
        public void start() {
            startTime = System.currentTimeMillis();
            running = true;
        }
     
        public void stop() {
            stopTime = System.currentTimeMillis();
            running = false;
        }
     
        //elaspsed time in milliseconds
        public long elapmili() {
            long elapsed;
            if (running) {
                 elapsed = (System.currentTimeMillis() - startTime);
            }
            else {
                elapsed = (stopTime - startTime);
            }
            return elapsed;
        }
     
    	public boolean isRunning()
    	{
    		return running;
    	}
     
        //elaspsed time in seconds
        public long elapSec() {
            long elapsed;
            if (running) {
                elapsed = ((System.currentTimeMillis() - startTime) / 1000);
            }
            else {
                elapsed = ((stopTime - startTime) / 1000);
            }
            return elapsed;
        }
     
     
    }
    Last edited by copeg; November 7th, 2010 at 02:49 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: working with multiple threads

    Could be you're calling stop too early or something.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: working with multiple threads

    Is a LinkedBlockingQueue syncrhonized? If not, you need to synchronize calls to the object so it isn't accessed concurrently.

Similar Threads

  1. Working with threads
    By tccool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 12th, 2010, 10:21 AM
  2. [SOLVED] Questions About Threads
    By neo_2010 in forum Threads
    Replies: 4
    Last Post: March 15th, 2010, 09:04 AM
  3. threads in gui
    By urosz in forum AWT / Java Swing
    Replies: 1
    Last Post: November 3rd, 2009, 05:20 PM
  4. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM