944,173 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1744
  • Java RSS
Feb 7th, 2005
0

Threads

Expand Post »
Hi everyone,

I have a question about threads. Consider the below program

Java Syntax (Toggle Plain Text)
  1.  
  2. public class test
  3. {
  4.  
  5. public void test1()
  6. {
  7. System.out.println("Test 1");
  8. }
  9.  
  10. public void test2()
  11. {
  12. System.out.println("Test 2");
  13. }
  14.  
  15. public void test3()
  16. {
  17. System.out.println("Test 3");
  18. }
  19.  
  20. public static void main(String args[])
  21. {
  22. test a = new Jtest();
  23. a.test1();
  24. a.test2();
  25. a.test2();
  26. }
  27. }

As you can see above i have three methods in the above class. My question is how do i call each of these methods in a separate thread either in the class main or in the class itself

Consider the below method

Java Syntax (Toggle Plain Text)
  1.  
  2. public void test()
  3. {
  4. System.out.println("Test 1"); //command line 1
  5. System.out.println("Test 2"); //command line 2
  6. }

As you can see from the above method i have two command lines in the above method. My question is how do i call each command line in a separate thread.

Basically i need to know how to call a specific method or a specific command line in a separate thread excluding the main thread

I hope someone can help me with both these questions

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Similar Threads
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Feb 7th, 2005
0

Re: Threads

You can pass a Runnable to a Thread.
Something like this will work
Java Syntax (Toggle Plain Text)
  1. // rest of your code ...
  2. public static void main(String[] args) {
  3. test t = new test();
  4. class R1 implements Runnable {
  5. test tt;
  6. public R1(test t) {tt=t;}
  7. public void run() {tt.test1();}
  8. }
  9. class R2 implements Runnable {
  10. test tt;
  11. public R2(test t) {tt=t;}
  12. public void run() {tt.test2();}
  13. }
  14. class R3 implements Runnable {
  15. test tt;
  16. public R3(test t) {tt=t;}
  17. public void run() {tt.test3();}
  18. }
  19. new Thread(new R1()).start();
  20. new Thread(new R2()).start();
  21. new Thread(new R3()).start();
  22. }

There are more elegant solutions using the reflection API.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Shifting Elements in an Array k places to the right.
Next Thread in Java Forum Timeline: Database not being populated....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC