Results 1 to 3 of 3
  1. #1
    charond is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Unhappy Can someone Help me with my semaphores on this Java file???

    Hey. I'm trying to get this file working to where I have to repeatedly print out three P's after every two F's, that is, FFPPPFFPPPFFPPPFFPPP. I'm fairly new to Java, so I don't have this down yet. I can add semaphore-related statements only. I cannot add any other statements such as assignments, printing statements or control structures, to name just a few. Can someone tell me what I am doing wrong, and how to make this work? Thanks!

    Here's the file:

    Java Code:
    import java.lang.Thread;
    import java.util.concurrent.*;
    
    public class ThreadSync
    {
    	private static boolean runFlag = true;
    	
        public static void main( String[] args ) {
    
            // create and name each runnable
            Runnable task1 = new TaskPrintP();
            Runnable task2 = new TaskPrintF();
            Runnable task3 = new IdleTask();
    
            Thread thread1 = new Thread( task1 );
            Thread thread2 = new Thread( task2 );
            Thread thread3 = new Thread( task3 );
    
            thread1.start();
            thread2.start();
            thread3.start();
    
            try {
    			Thread.sleep(500);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
            
            runFlag=false;
     
            thread3.interrupt();
            thread2.interrupt();
            thread1.interrupt();
     
        }
        
        public static class TaskPrintP implements Runnable 
        {
            public void run(){
        	    while (runFlag) {
        	        System.out.printf( "%s\n", "P");
        	    }
            }
        }
        public static class TaskPrintF implements Runnable 
        {
            public void run(){
            	while (runFlag) {
        	        System.out.printf( "%s\n", "F");
        	    }
            }
        }
        public static class IdleTask implements Runnable 
        {
            public void run(){
            	while (runFlag) {
        	    }
            }
        }
    }
    Last edited by quad64bit; 10-30-2012 at 11:59 PM. Reason: clarity

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Can someone Help me with my semaphores on this Java file???

    Please use [code] tags [/code] when posting code.
    Many people won't bother reading unformatted code.

    What errors are you getting?
    What line(s) are they occurring on?
    What do you see happening? What should you see happening (actually, you've answered this bit)?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Can someone Help me with my semaphores on this Java file???

    Well, step 1. You need a semaphore. There is a Semaphore class in Java Semaphore (Java Platform SE 7 ), but your third thread makes me think that you're supposed to create it.... but maybe not.

    So basically (VERY BASIC): Thread1 says to semaphore, "Hey, I want to do some critical stuff."
    Semaphore says, "I have a permit, here you go.".
    Thread2 says, "Hey, I want to do some critical stuff.".
    Semaphore says, "No permit available." (Thread2 waits)
    Thread1 says, "I'm done, here's your permit back."
    Semaphore says (to Thread2), "I have your permit." ....

Similar Threads

  1. Java semaphores, a question/bug regarding Signal.
    By Ameer3881 in forum Advanced Java
    Replies: 7
    Last Post: 04-10-2012, 03:02 PM
  2. Semaphores
    By Ameer3881 in forum New To Java
    Replies: 9
    Last Post: 04-10-2012, 02:39 AM
  3. Replies: 1
    Last Post: 07-28-2011, 07:50 PM
  4. Semaphores
    By MuslimCoder in forum Threads and Synchronization
    Replies: 0
    Last Post: 04-15-2010, 05:55 AM
  5. Waiting on multiple semaphores at the same time
    By flok in forum Threads and Synchronization
    Replies: 3
    Last Post: 11-10-2009, 03:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •