Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-25-2007, 03:37 AM
Member
 
Join Date: Apr 2007
Location: Pennsylvania,USA
Posts: 46
sandor is on a distinguished road
threads question
I am trying to learn how to use threads to run a couple of methods repeatedly. This is my code and I get a compilation error when I try to run it. I am confused. help.
Code:
public void doA() { System.out.println("now I am doing A"); } public void doB() { System.out.println("now I am doing B"); } public void sleep() { System.out.println( " Zzzzzzz..."); } public void live() { new Thread() { public void run() { while (true) { doA(); doB(); sleep(); } } }.start(); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-25-2007, 03:50 AM
Member
 
Join Date: Apr 2007
Location: USA
Posts: 50
derrickD is on a distinguished road
Threads are hard. I think that the sleep method that this anonymous class is using is its sleep() method which needs a parameter passed in. I do not think it is using the sleep method you created. I changed your method name to doze() and changed it in the anonymous class and it ran , but of course it never stopped running. Somewhere you need to give it a time to stop, unless of course it is something you want to run forever, which, I supposed, you might
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-25-2007, 06:09 PM
Member
 
Join Date: Apr 2007
Location: Indiana
Posts: 84
pegitha is on a distinguished road
Send a message via Skype™ to pegitha
You could also use the sleep(long) method instead of yours.
Code:
long interim=1000; public void live() { new Thread() { public void run() { while (true) { doA(); doB(); try { sleep(interim); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-29-2007, 04:57 PM
Member
 
Join Date: Apr 2007
Location: Pennsylvania,USA
Posts: 46
sandor is on a distinguished road
Thanks to both of you.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-04-2007, 10:49 AM
Member
 
Join Date: Dec 2007
Posts: 1
Maris is on a distinguished road
can anyone help me?I have one question i.e what is the difference between start() and run.we can run a thread by thread.run() then why we need start().
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-10-2007, 07:24 AM
Member
 
Join Date: Dec 2007
Posts: 3
ingle.anil is on a distinguished road
sequential execution
thread.start() -- concurrent execution
1)create new thread and
2)then calls run() method.

thread.run() -- sequential execution
1)calls run() method.
- no new Thread creation.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-21-2008, 11:31 AM
Member
 
Join Date: Jun 2008
Posts: 7
rorootot is on a distinguished road
One more question.
Why in front of start () is put dot, like that ''.start()''?
Does in threads is obligatory to do like that?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-21-2008, 05:04 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
Norm is on a distinguished road
That is the syntax for calling an object's method: <object-ref>.<method>...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
applets & threads willemjav Java Applets 2 04-04-2008 07:59 AM
Using threads Java Tip Java Tips 0 12-11-2007 11:25 AM
Threads one198 Threads and Synchronization 1 11-20-2007 07:15 PM
Question mark colon operator question orchid Advanced Java 3 04-30-2007 11:37 PM
Server n threads ferosh Networking 2 04-28-2007 11:42 PM


All times are GMT +3. The time now is 09:56 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org