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

Thread: I'm vary new to java programming and confused about som concepts.

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I'm vary new to java programming and confused about som concepts.

    To create threading there is an object

    Thread.sleep();

    Is Thread is an object ? sleep is a method? I never created a object called Thread then how can it exists.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    Thread is a class and sleep is a static method in that class.
    Have you read the API doc for the Thread class?
    The API doc is here: Java Platform SE 6
    Find the Thread class in the lower left and click on it to bring the doc into the righthand frame.
    Read in the tutorials about static methods and variables.

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    java.lang.Thread is a class. The Java compiler automatically resolves references to java.lang.* classes as though you had included the line "import java.lang.*;" in your source.

    Thread (Java 2 Platform SE 5.0)

    sleep(int) is declared static, so you can access the method from the class without needing an instance of Thread.

    The Thread API is a bit of a mess. I wish the Sun developers had made sleep a member method so that to sleep your current thread, you'd have to use "Thread.currentThread().sleep(1000);" - which is probably what the current method does internally. Note that you can do this anyway, but the reference from currentThread() would be redundant.

    edit: 1 minute later than Norm as usual. I'm going to have to start my own Java programming forum

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    Yea since sleep is a static method?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    Are you asking a question?

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    yes because I'm very new to java.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    What is the question?

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    you answered in #2 that what I understood is sleep is a static method hence it invoked with out actually creating an object reference variable. right?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    right?
    Sorry, I don't understand your question. Do you understand the previous posts?

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    I'm asking what I have understand is right or wrong?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    I understood is sleep is a static method hence it invoked with out actually creating an object reference variable
    yes that sounds right.

  12. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: I'm vary new to java programming and confused about som concepts.

    If you are beginner, you must not start with threads but with classes first.

  13. #13
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    sleep example:

    ...
    thread.sleep(2000);
    textarea.append("this Message displays after 2 seconds!");
    thread.sleep(5000);
    textarea.append("This Message displays after 5 seconds!");
    thread.sleep(1000);
    System.out.println("This Message will display on the console after 1 second!");
    ...

    Although i wouldn't recommend using sleep when appending to a JTextArea, Due to it causing your program to freeze for the ammount of sleep time. Instead you would use a "timer".

    Timer example on oracle: http://download.oracle.com/javase/tu...isc/timer.html

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    @macko
    What happens when you execute the code you posted?
    Does it do what you want it to do? What do you want the code to do?

  15. #15
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: I'm vary new to java programming and confused about som concepts.

    Quote Originally Posted by Norm View Post
    @macko
    What happens when you execute the code you posted?
    Does it do what you want it to do? What do you want the code to do?
    I guess he just tried to present the sleep() concept, as it puts your thread to sleep condition for number of milliseconds/nanoseconds.

  16. #16
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: I'm vary new to java programming and confused about som concepts.

    @Mr.777
    correct....

    Just a demonstration of a spot never to use a sleep

Similar Threads

  1. Newbie: seem confused with Java
    By boyscout in forum Java Theory & Questions
    Replies: 1
    Last Post: April 8th, 2011, 09:12 AM
  2. [SOLVED] Java beginner is confused....
    By truebluecougarman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 27th, 2011, 08:50 AM
  3. Java File IO question :confused:
    By byebyebye in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: August 17th, 2010, 06:45 AM
  4. Replies: 5
    Last Post: June 10th, 2010, 10:19 AM
  5. Confusion about Java development IDES
    By neo_2010 in forum The Cafe
    Replies: 4
    Last Post: July 7th, 2009, 03:14 PM