Results 1 to 3 of 3
- 10-30-2012, 02:34 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 1
- Rep Power
- 0
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
- 10-30-2012, 02:55 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 10-30-2012, 07:54 PM #3
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
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
-
Java semaphores, a question/bug regarding Signal.
By Ameer3881 in forum Advanced JavaReplies: 7Last Post: 04-10-2012, 03:02 PM -
Semaphores
By Ameer3881 in forum New To JavaReplies: 9Last Post: 04-10-2012, 02:39 AM -
the mp3 file is not working..java.io.IOException: File Not Found......javax.media.NoP
By shruts in forum Java AppletsReplies: 1Last Post: 07-28-2011, 07:50 PM -
Semaphores
By MuslimCoder in forum Threads and SynchronizationReplies: 0Last Post: 04-15-2010, 05:55 AM -
Waiting on multiple semaphores at the same time
By flok in forum Threads and SynchronizationReplies: 3Last Post: 11-10-2009, 03:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks