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 11 of 11

Thread: two programs won't synchronize! Plz help!!!

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post two programs won't synchronize! Plz help!!!

    Hello there.

    I've been racking my fruitless brains for hours now, but I still have two huge problems with my code.

    // -------- Begin: Advertisement.java ------------
    class Advertisement extends Thread
    {
    	String msg = "";
    	synchronized Advertisement(String str)
    	{
    		msg = str;
    	}
        public void run()
        {
            while(true)
            {
                try
                {
                    System.out.print(msg + " ");
                    sleep(1000);
                }
                catch (InterruptedException e)
                {
    				break;
                }
            }
        }
    }
    // --------End: Advertisement.java ------------
    // -------- Begin: TestAdvt.java ------------
    import java.io.*;
     
    class TestAdvt
    {
        public static void main (String[] args)
        {
    		try
    		{
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			while(true)
    			{
    				System.out.println("Please enter the advertisement message to be displayed (enter 'n' to exit):");
    				String str = br.readLine();
    				if (str == "n") break;
    				else{ 
    					Advertisement advt = new Advertisement(str);
    					advt.start();
    					System.in.read();
    					advt.interrupt();}
    			}
    			br.close();
    		}
    		catch (IOException e)
    		{
                System.out.println("error: " + e.getMessage());
    		}
        }
    }
    // --------End: TestAdvt.java ------------





    1. The programs aren't synchronized, so when I run it and press enter in the middle of the announcements, it keeps printing the msg + " " (I want Advertisement.java to pause until the user inputs another message)
    2. For some reason, even if I enter "n" (which should allow the user to exit the program completely) the program won't go through the if-statement.

    I'm a real beginner at this, so I'd really appreciate simple language (no advanced stuff)

    Thank you!
    Last edited by yihyang; April 4th, 2013 at 11:24 AM. Reason: Forgot to add the code :P


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: two programs won't synchronize! Plz help!!!

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    yihyang (April 4th, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: two programs won't synchronize! Plz help!!!

    Thank you!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: two programs won't synchronize! Plz help!!!

    Does the code execute the call to the interrupt() method? Add a println that prints a message to see.

    The code should use the equals() method to compare Strings, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    yihyang (April 4th, 2013)

  7. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: two programs won't synchronize! Plz help!!!

    Quote Originally Posted by Norm View Post
    Does the code execute the call to the interrupt() method? Add a println that prints a message to see.
    I've done that (after taking out the 'synchronized' keyword in front of the Advertisement constructor), but it just prints out:

    message message message
    Please enter the advertisement message to be displayed:
    Interrupted

    and it continues to print blank messages every second...

    --- Update ---

    Quote Originally Posted by Norm View Post
    The code should use the equals() method to compare Strings, not the == operator.
    Yay! The break works! thx

  8. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: two programs won't synchronize! Plz help!!!

    Any suggestions??? Really need help here ><

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: two programs won't synchronize! Plz help!!!

    Please explain what the problem is now?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: two programs won't synchronize! Plz help!!!

    Quote Originally Posted by Norm View Post
    Please explain what the problem is now?
    "Does the code execute the call to the interrupt() method? Add a println that prints a message to see."

    I've done that (after taking out the 'synchronized' keyword in front of the Advertisement constructor), but it just prints out:

    message message message
    Please enter the advertisement message to be displayed:
    Interrupted

    and it continues to print blank messages every second...

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: two programs won't synchronize! Plz help!!!

    Can you post the current version that shows the println() statements.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #10
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: two programs won't synchronize! Plz help!!!

    I've taken care of the synchronizing problem (I just limited the access to the while loop in Advertisement), but now, when I run the program, starting from the second loop, the program ignores the first letter of all inputs

    For example, it prints like this:
    Please enter the advertisement message:
    Attention
    ttention ttention ttention

    // -------- Begin: Advertisement.java ------------
    class Advertisement extends Thread
    {
    	String msg = "";
     
    	public Advertisement(String str) {msg = str;}
        public void run()
        {
    		boolean cont = false;
    		if (!msg.equals("")) cont = true;
            while(cont)
            {
                try
                {
                    System.out.print(msg + " ");
                    sleep(1000);
                }
                catch (InterruptedException e)
                {
    				System.out.println("Interrupted");
    				break;
                }
            }
        }
    }
    // --------End: Advertisement.java ------------
    // -------- Begin: TestAdvt.java ------------
    import java.io.*;
     
    class TestAdvt
    {
        public static void main (String[] args)
        {
    		try
    		{
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			while(true)
    			{
    				System.out.println("Please enter the advertisement message to be displayed (enter 'n' to exit):");
    				String str = br.readLine();
    				if (str.equals("n")) break;
    				else{ 
    					Advertisement advt = new Advertisement(str);
    					advt.start();
    					System.in.read();
    					advt.interrupt();}
    			}
    			br.close();
    		}
    		catch (IOException e)
    		{
                System.out.println("error: " + e.getMessage());
    		}
        }
    }
    // --------End: TestAdvt.java ------------

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: two programs won't synchronize! Plz help!!!

    the program ignores the first letter of all inputs
    I'm not sure what reading from two different inputs will do.
    System.in.read();

    Here's the console when I execute it:
    Please enter the advertisement message to be displayed (enter 'n' to exit):
    msg1 sent interrupt at Thu Apr 04 16:59:31 EDT 2013
    Please enter the advertisement message to be displayed (enter 'n' to exit):
    Interrupted at Thu Apr 04 16:59:32 EDT 2013
    sent interrupt at Thu Apr 04 16:59:32 EDT 2013
    Please enter the advertisement message to be displayed (enter 'n' to exit):
    sent interrupt at Thu Apr 04 16:59:32 EDT 2013
    Please enter the advertisement message to be displayed (enter 'n' to exit):
    msg2 Interrupted at Thu Apr 04 16:59:32 EDT 2013
    msg3 Interrupted at Thu Apr 04 16:59:32 EDT 2013
    I used this in the main() method to execute with
             System.setIn(new ByteArrayInputStream("msg1\nmsg2\nmsg3\nn\n".getBytes()));
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Greetings programs
    By neveser in forum Member Introductions
    Replies: 2
    Last Post: December 7th, 2012, 10:41 PM
  2. Using i-1 in programs
    By JoshKesner in forum Java Theory & Questions
    Replies: 4
    Last Post: November 7th, 2012, 06:01 PM
  3. Parallel vectors synchronize
    By speaker in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 18th, 2012, 04:27 PM
  4. When to Synchronize Code
    By Bijaysadhok in forum Threads
    Replies: 12
    Last Post: March 16th, 2012, 10:28 AM
  5. Student Programs
    By Suzanne42 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 26th, 2011, 10:20 AM