Results 1 to 10 of 10
  1. #1
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default How to run two methords at the same time

    Hello,

    Im new to Java multithreading and have been expermenting. Ive came up with the following code which is very simple. What i would like to do is create two syncorized methords that print out some text. I would like to call these methords on differnet thread. Can anyone show me on how to do this?

    Java Code:
    import java.lang.Thread;
    import java.lang.Runnable;
    import java.lang.InterruptedException;
    /**
     *
     * @author  */
    public class Threads implements Runnable {
        
        String name;
        Thread t;
        
        Threads(String threadname) {
            name = threadname;
            t = new Thread(this, name);
            System.out.println("New thread: " + t);
            t.start();        
        }
        
        public void run() {
            try {
                for (int i = 5; i > 0; i--) {
                    System.out.println(name + ": " + i);
                    Thread.sleep(12000);
                }
            }catch (InterruptedException e) {
                    e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            Threads ob1 = new Threads("One");
            Threads ob2 = new Threads("Two");
            Threads ob3 = new Threads("Three");
            
            System.out.println("Thread " + ob1.name + " is alive: " + ob1.t.isAlive());
            System.out.println("Thread " + ob2.name + " is alive: " + ob2.t.isAlive());
            System.out.println("Thread " + ob3.name + " is alive: " + ob3.t.isAlive());
            
            try {
                System.out.println("Waiting for Threads to finish");
                ob1.t.join();
                ob2.t.join();
                ob3.t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("Thread " + ob1.name + " is alive: " + ob1.t.isAlive());
            System.out.println("Thread " + ob2.name + " is alive: " + ob2.t.isAlive());
            System.out.println("Thread " + ob3.name + " is alive: " + ob3.t.isAlive());
            
            System.out.println("Main Thread Exiting");
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: How to run two methords at the same time

    Sooo just call the method from the Threads' run method, right?

    Also, you can't run synchronized methods at the same time. That's a contradiction.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default Re: How to run two methords at the same time

    Hi KevinWorkman

    I've modified the code to call two methords that just print to the screen. As you can see here i have two threads called ob1 and ob2. How can i get ob1 to call methord Output1 and ob2 to call methord Output2 thus each thread executing a differnet methord.

    Java Code:
    package threads;
    import java.lang.Thread;
    import java.lang.Runnable;
    import java.lang.InterruptedException;
    /**
     *
     * @author steven.haynes
     */
    public class Threads implements Runnable {
        
        String name;
        Thread thread;
        boolean available = true;
        
        Threads(String threadname) {
            name = threadname;
            thread = new Thread(this, name);
            System.out.println("New thread: " + thread);
            thread.start();        
        }
        
        public synchronized void Output1() {
            while (available == false) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
            available = true;
            notifyAll();
            System.out.println("Output 1");
        }
        
        public synchronized void Output2() {
            while (available == false) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
            available = false;
            notifyAll();
            System.out.println("Output 2");
        }
        
        public void run() {
            Output1();
            Output2();
        }
    
        public static void main(String[] args) {
            Threads ob1 = new Threads("One");
            Threads ob2 = new Threads("Two");
            
            System.out.println("Thread " + ob1.name + " is alive: " + ob1.thread.isAlive());
            System.out.println("Thread " + ob2.name + " is alive: " + ob2.thread.isAlive());
            
            try {
                System.out.println("Waiting for Threads to finish");
                ob1.thread.join();
                ob2.thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("Thread " + ob1.name + " is alive: " + ob1.thread.isAlive());
            System.out.println("Thread " + ob2.name + " is alive: " + ob2.thread.isAlive());
            
            System.out.println("Main Thread Exiting");
        }
    }
    OUTPUT
    Java Code:
    New thread: Thread[One,5,main]
    New thread: Thread[Two,5,main]
    Thread One is alive: true
    Thread Two is alive: true
    Waiting for Threads to finish
    Output 1
    Output 2
    Output 1
    Output 2
    Thread One is alive: false
    Thread Two is alive: false
    Main Thread Exiting

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: How to run two methords at the same time

    Well you're going to need two instances of Runnable, or two subclasses of Thread then. Recommended reading: Defining and Starting a Thread (The Java™ Tutorials > Essential Classes > Concurrency)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default Re: How to run two methords at the same time

    I've changed the code. Is it possible to syncroizre these methords by using wait() and notifyAll()? Would there be any performance issue here for larger methords, if so what improvments could be made?

    Java Code:
    package threads;
    import java.lang.Thread;
    import java.lang.Runnable;
    import java.lang.InterruptedException;
    /**
     *
     * @author steven.haynes
     */
    
    public class Threads {
        
        private static class Threads1 implements Runnable {
            
            Threads1() {
            }
        
            public void Output1() {
                System.out.println("Output 1");
            }
        
            public void run() {
                Output1();
            }
        }
    
        private static class Threads2 implements Runnable {
            
            Threads2() {
            }
            
            public void Output2() {
                System.out.println("Output 2");
            }
        
            public void run() {
                Output2();
            }
        }
        
    
        public static void main(String[] args) {
            Thread ob1 = new Thread(new Threads1());
            Thread ob2 = new Thread(new Threads2());
            ob1.start();
            ob2.start();
            
            System.out.println("Thread " + ob1.getName() + " is alive: " + ob1.isAlive());
            System.out.println("Thread " + ob2.getName() + " is alive: " + ob2.isAlive());
            
            try {
                System.out.println("Waiting for Threads to finish");
                ob1.join();
                ob2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("Thread " + ob1.getName() + " is alive: " + ob1.isAlive());
            System.out.println("Thread " + ob2.getName() + " is alive: " + ob2.isAlive());
            
            System.out.println("Main Thread Exiting");
        }
    }
    OUTPUT
    Thread Thread-0 is alive: true
    Thread Thread-1 is alive: true
    Waiting for Threads to finish
    Output 1
    Output 2
    Thread Thread-0 is alive: false
    Thread Thread-1 is alive: false
    Main Thread Exiting

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: How to run two methords at the same time

    Why do you want to synchronize them? If you're synchronizing them, why use threads at all? What exactly do you want to happen?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default Re: How to run two methords at the same time

    No reason just experimenting.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: How to run two methords at the same time

    Okay, well, what you're saying doesn't make a ton of sense to me. Basically, calling two methods from two different threads causes them to happen at the same time (or appear to, at least). Synchronizing the methods prevents them from happening at the same time. You're basically cancelling out any benefit that threading gives you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default Re: How to run two methords at the same time

    If i wanted to have control over my threads i would synchronize them?

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: How to run two methords at the same time

    I don't know, maybe, it depends, and your example doesn't give enough information to give a straight answer. There are quite a few things you can do to have control over your threads.

    Recommended reading: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. making a Time class but Universal time always says 0
    By Bravid2001 in forum New To Java
    Replies: 2
    Last Post: 10-24-2011, 07:55 AM
  2. Time lost during time measuring
    By Kobe in forum Advanced Java
    Replies: 6
    Last Post: 08-29-2011, 03:18 PM
  3. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  4. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 PM
  5. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM

Tags for this Thread

Posting Permissions

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