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

Thread: threads

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

    Default threads

    I have the following code on threads.I have a doubt that is it possible that any of the two threads created ie one and two will execute the 2nd print statement in run method before the first print statement in it?That is print hello first and then the current thread name

    public class HelloWorld {
    public static void main(String []args){
    System.out.println("Hello World");
    Runnable r = new Animal();
    Thread t =new Thread(r);
    Thread t1 =new Thread(r);
    t.start();
    t.setName("one");
    t1.start();
    t1.setName("two");
    System.out.println(Thread.currentThread().getName( ));
    }
    }
    class Animal implements Runnable{
    public void run(){
    System.out.println(Thread.currentThread().getName( ));
    System.out.println("hello");
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: threads

    I'm not sure what your question is, but I recommend reading through this tutorial: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: threads

    Quote Originally Posted by KevinWorkman View Post
    I'm not sure what your question is, but I recommend reading through this tutorial: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    I am asking that whether it is possible in the above code to have output as :
    hello
    one
    main
    hello
    two Because print statement for hello occurs after print for thread name.So is it possible for a thread to skip a statement and jump to its next statement and then execute the previous statement as above output??

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: threads

    I'm not sure why you'd want to do this, other than if this is a homework assignment. Either way, the tutorial I linked to you contains explanations for everything you'd need to do.

    Keep in mind that threads don't have any magic rules that normal code doesn't. There is no "jumping" around in the code, other than the normal ways. Also, keep in mind that just because you're seeing a certain output most of the time, that doesn't mean it'll work that way every time you run it- you have no synchronization over here, so you can't control (or predict) which order things happen in.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Use of threads in an animation
    By 2by4 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 15th, 2011, 09:47 AM
  2. Threads in some order...
    By aps135 in forum Threads
    Replies: 6
    Last Post: March 11th, 2011, 05:54 PM
  3. threads
    By crazed8s in forum Threads
    Replies: 2
    Last Post: December 14th, 2010, 05:33 AM
  4. Threads and JDBC
    By shorawitz in forum Threads
    Replies: 1
    Last Post: November 11th, 2010, 09:24 PM
  5. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM

Tags for this Thread