Results 1 to 5 of 5
  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Advice on a homework quesiton

    Hellow again folks. I have a homework question that asks me to implement two classes that sets two concurrent threads running, one implementing Runnable and the other extending Thread. This part is ok, and it does work (no need to point out the dangers of doing this type of implementation in the real world, that's already implied). Anyway, here is the code I've written;
    The main method;

    Java Code:
    package inout;
    
    public class Main {
    
    
        public static void main(String[] args)
        {
           In in = new In();
           Thread t1 = new Thread(in);
           Out out = new Out("Out the door");
           t1.start();
           out.start();
        }
    
    }
    The In class:

    Java Code:
    package inout;
    
      public class In implements Runnable
    {
        private static final int PRINT_IN = 5;
        private String in = "In *";
        public void run()
        {
            for (int i=0; i< PRINT_IN; i++)
            {
                System.out.print(in);
            }
            try
            {
                Thread.sleep(20);
            }
            catch(InterruptedException e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
    The out class:

    Java Code:
    package inout;
    
    public class Out extends Thread
    {
    private static final int PRINT_OUT = 4;
    private String out = "Out *";
    
    
    public Out(String thisThread)
        {
        super();
        Thread t2 = new Thread();
        t2.setName(thisThread);
    
    }
    public void run()
        {
        for (int i = 0; i<PRINT_OUT; i++)
        {
            System.out.print(out);
        }
        try
            {
                Thread.sleep(20);
            }
            catch (InterruptedException e)
            {
                System.out.println(e.getMessage());
            }
    }
    }
    The advice I'm looking for is this. The next part of the question askes me to instruct main to wait until both the threads have executed, then print out a line. The only way I can think of doing this within the main class is to create an inner class that also implements Runnable with a method that will wait while t1.isAlive() && out.isAlive() to be false. Is this the correct implementation in your opinion or am I missing something simple? There doesn't seem to be enough of a points allocation to merit this amount of effort. It also advises to look at the Thread API for a suitable method to implement (while() is inhereted from Object at this point). I can't implement while() directly as this will cause a "non static method while() cannot be referenced from a static context" error.

    Thanks in advance

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Advice on a homework quesiton

    Look at the Thread class's join() method.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Advice on a homework quesiton

    Quote Originally Posted by Norm View Post
    Look at the Thread class's join() method.
    Hi again Norm

    Yea, I was looking down that road but not too sure how to implement it. I ended up with this:

    Java Code:
    try
           {
               Thread.sleep(1000);
           }
           catch (Exception e)
           {
               System.out.println(e.getMessage());
           }
           System.out.println();
           System.out.println("FINISHED");
        }
    In the main method but this doesn't seem right to me. How do I know that 1000ms is enough for the threads to finish their jobs? I may well be over analysing this but I think your right. Will keep plugging away at it when time isn't so critical.

    Thanks again for the advice. Do you have any examples of join() implementations that I can look at?

    Cheers

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Advice on a homework quesiton

    No I don't have any examples. Probably some on the forum, try Searching.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

Similar Threads

  1. Specimen exam quesiton
    By elliotHenry in forum New To Java
    Replies: 14
    Last Post: 06-14-2011, 09:18 PM
  2. Need some help with my homework.
    By JacobM in forum New To Java
    Replies: 11
    Last Post: 02-04-2011, 02:42 AM
  3. Homework help please
    By chick in forum New To Java
    Replies: 22
    Last Post: 03-19-2010, 07:39 AM
  4. Please Help with Homework
    By theuser in forum Advanced Java
    Replies: 2
    Last Post: 07-30-2009, 03:37 PM
  5. Need help with homework.
    By JavaNewbie0000 in forum New To Java
    Replies: 2
    Last Post: 07-31-2008, 03:06 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
  •