Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: how to run the two threads sequentially

  1. #1
    nirmalav is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default how to run the two threads sequentially

    hi all,
    I have two threads by name T1, T2. T1 thread will generate the 10 multiplication table. T2 will generate the 20 multiplication table. my problem is i would like to control over the threads. I just want to run the T1 first and and then T2. Please find the following code which i have written. Any help would be great help for me. Thanks in Adavance


    public class ThreadTestFire extends Thread{

    /**
    * @param args
    */
    public ThreadTestFire(Runnable r){
    super(r);
    }
    public static void main(String[] args) throws InterruptedException{
    // TODO Auto-generated method stub
    synchronized (ThreadTestFire.class) {
    new ThreadTestFire(new MyThread(10)).start();

    //Thread.currentThread().notifyAll();
    }
    synchronized (ThreadTestFire.class) {
    new ThreadTestFire(new MyThread(20)).start();
    }

    synchronized (ThreadTestFire.class) {
    new ThreadTestFire(new MyThread(30)).start();
    }
    //Thread.currentThread().join();
    }

    }

    MyThread Class
    *****************************
    public class MyThread implements Runnable {
    private int x;

    public MyThread(int x){
    this.x = x;
    }

    @Override
    public void run() {
    int y = 0;
    for(int i = 1; i <=10; i++){
    y = this.x * i;
    System.out.println( i +" * " + this.x + "=" + y);
    try {
    Thread.currentThread().sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    }
    *********************************************


    regards & thanks,
    Nirmala Vijaya Sekhar Varre

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: how to run the two threads sequentially

    Quote Originally Posted by nirmalav View Post
    I just want to run the T1 first and and then T2.
    So what's the point in using threads at all?
    DarrylBurke likes this.
    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
    nirmalav is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: how to run the two threads sequentially

    Hi,
    Thank you for the response. But i just want to have the control over the thread. I just want to run the thread as i like not as JVM/OS like.
    I just want to understand the concept of Synchronization.
    regards & thanks,
    Nirmala Vijaya Sekhar Varre

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

    Default Re: how to run the two threads sequentially

    If you want to run them sequentially, you probably shouldn't be using threads. There are other ways around it, but it really doesn't make sense to use threads if you always want to do things sequentially.

    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!

  5. #5
    nirmalav is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: how to run the two threads sequentially

    hi,
    i got it what you are saying but i just want catch hold of a thread one thread and i want to have control over the thread which are currently running in my app. if you have any idea please do share with me.

    regards
    ~vijay

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: how to run the two threads sequentially

    Did you read the tutorial I gave you? It contains plenty of information, including how to accomplish what you're asking about.
    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
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to run the two threads sequentially

    Consider using an Executor obtained via Executors.newSingleThreadScheduledExecutor(). The API states:
    Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newScheduledThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.

  8. #8
    nirmalav is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: how to run the two threads sequentially

    KevinWorkman,

    i am pretty new this forum..so please provide me link to your tutorial which you have mentioned about in ur previous response.
    regards,
    ~vijay

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to run the two threads sequentially

    Quote Originally Posted by nirmalav View Post
    KevinWorkman,

    i am pretty new this forum..so please provide me link to your tutorial which you have mentioned about in ur previous response.
    regards,
    ~vijay
    Um... he already did -- please read all his posts. I also gave you a link to an API.

  10. #10
    rfq
    rfq is offline Member
    Join Date
    Mar 2012
    Location
    Novosibirsk, Russia
    Posts
    15
    Rep Power
    0

    Default Re: how to run the two threads sequentially

    Quote Originally Posted by nirmalav View Post
    But i just want to have the control over the thread. I just want to run the thread as i like not as JVM/OS like.
    I just want to understand the concept of Synchronization.
    Threads are controlled in 2 ways:
    1) programmer feeds threads with resources and input information
    2) JVM/OS makes best to run them in parallel, as long as resources/information are available

    The concept of synchronization is to provide correct way to pass resources/information to threads: the act of passing takes some time, and during that time only one thread have access to the resources being passed.

    Since your threads do not exchange information, there is no room for synchronization. Trying to control threads in this situation means building a thread framework upon existing JVM/OS implementation. This is an interesting task, but to implement it, you should clearly understand in what way you are not satisfied with existing implementation. As far as I can see, this task is not what you should do just now.

  11. #11
    mjgood91 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default Re: how to run the two threads sequentially

    Why don't you just stick a speep() command between each thread start? This will start each thread slightly after the last one, so all threads will fire x many milliseconds after the last one is done...

    Java Code:
        public static void main(String[] args) throws InterruptedException
        {
            // TODO Auto-generated method stub
            synchronized (ThreadTestFire.class) 
            {
                new ThreadTestFire(new MyThread(10)).start();    
        
                //Thread.currentThread().notifyAll();
            }
            sleep (5);
            synchronized (ThreadTestFire.class) 
            {
                new ThreadTestFire(new MyThread(20)).start();
            }
            sleep (5);
            synchronized (ThreadTestFire.class) 
            {
                new ThreadTestFire(new MyThread(30)).start();
            }
        //Thread.currentThread().join();
        }
    Not sure if this is kinda destroying the point of using synchronization in the first place, but it does what you want it to do...

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: how to run the two threads sequentially

    Quote Originally Posted by mjgood91 View Post
    but it does what you want it to do...
    Are you sure about that? Only the main thread will access that method, so I'm not sure what those synchronized blocks are for. And you are delaying the start of each thread, but not their completion. For example, if each thread was going to perform an action that took somewhere between an hour and a day, you could not guarantee that the first thread goes first, then the second one, then the third one, which I believe is what the OP wants.

    The answer is in the tutorial I linked to. But really, the answer is more a question- why would you bother using threads if you're just going to keep them sequential?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to run the two threads sequentially

    Kevin, I think that there are applications for this sort of thing, for instance if you want to use a queue to hold Runnables or Futures and allow them to be processed sequentially in a background thread.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: how to run the two threads sequentially

    Moved from Advanced Java for better topic alignment.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: how to run the two threads sequentially

    Quote Originally Posted by Fubarable View Post
    Kevin, I think that there are applications for this sort of thing, for instance if you want to use a queue to hold Runnables or Futures and allow them to be processed sequentially in a background thread.
    Oh yeah, I absolutely agree with that. There are definitely times when you want to control thread execution, which is why I linked to the concurrency tutorial. But for this case, I'm not sure the OP should be using threads at all. I would predict that he's misunderstanding an assignment (or maybe the assignment is a purposely bad use of threading), but that's just a guess. Either way, the "how" to do this is covered in the tutorial, regardless of the "why", which is the part I'm curious about.
    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. Threads per Connection or Threads per Request
    By Felic in forum New To Java
    Replies: 4
    Last Post: 11-22-2011, 10:15 PM
  2. Threads!!!
    By smartino in forum New To Java
    Replies: 67
    Last Post: 11-13-2011, 06:24 PM
  3. The right way of using threads
    By N00Bie in forum New To Java
    Replies: 9
    Last Post: 02-20-2011, 05:39 PM
  4. GUI and Threads
    By rp181 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-10-2009, 09:39 PM
  5. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 11:25 AM

Posting Permissions

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