Results 1 to 10 of 10
Thread: threads question
- 04-25-2007, 02:37 AM #1
Member
- Join Date
- Apr 2007
- Location
- Pennsylvania,USA
- Posts
- 45
- Rep Power
- 0
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.
Java 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(); }
- 04-25-2007, 02:50 AM #2
Member
- Join Date
- Apr 2007
- Location
- USA
- Posts
- 50
- Rep Power
- 0
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
- 04-25-2007, 05:09 PM #3
Member
- Join Date
- Apr 2007
- Location
- Indiana
- Posts
- 83
- Rep Power
- 0
You could also use the sleep(long) method instead of yours.
Java 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(); }
- 04-29-2007, 03:57 PM #4
Member
- Join Date
- Apr 2007
- Location
- Pennsylvania,USA
- Posts
- 45
- Rep Power
- 0
Thanks to both of you.
- 12-04-2007, 09:49 AM #5
Member
- Join Date
- Dec 2007
- Posts
- 1
- Rep Power
- 0
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().
- 12-10-2007, 06:24 AM #6
Member
- Join Date
- Dec 2007
- Posts
- 3
- Rep Power
- 0
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.
- 07-21-2008, 10:31 AM #7
Member
- Join Date
- Jun 2008
- Posts
- 7
- Rep Power
- 0
One more question.
Why in front of start () is put dot, like that ''.start()''?
Does in threads is obligatory to do like that?
- 07-21-2008, 04:04 PM #8
That is the syntax for calling an object's method: <object-ref>.<method>...
- 02-07-2009, 08:46 PM #9
Member
- Join Date
- Feb 2009
- Posts
- 1
- Rep Power
- 0
Producer Consumer
Hello All,
I am new to this community as well as Java.
I need to understand how threading works in Java and have to imolement the code for hungry birds problem and The Bear and the Honeybees problem in Java using semaphore.
help needed.
- 02-07-2009, 08:57 PM #10
You should open a new post.... this post (the one you used) is very old (almost two years). As for your question about threads and semaphores, here's a couple of links:
Processes and Threads (The Java Tutorials > Essential Classes > Concurrency)
Semaphore (Java 2 Platform SE 5.0)
Programming Examples: Java Semaphore
Example Java classes
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
applets & threads
By willemjav in forum Java AppletsReplies: 2Last Post: 04-04-2008, 06:59 AM -
Using threads
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 10:25 AM -
Threads
By one198 in forum Threads and SynchronizationReplies: 1Last Post: 11-20-2007, 06:15 PM -
Server n threads
By ferosh in forum NetworkingReplies: 2Last Post: 04-28-2007, 10:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks