| | |
Threads
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 8
Hi everyone,
I have a question about threads. Consider the below program
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
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
I have a question about threads. Consider the below program
Java Syntax (Toggle Plain Text)
public class test { public void test1() { System.out.println("Test 1"); } public void test2() { System.out.println("Test 2"); } public void test3() { System.out.println("Test 3"); } public static void main(String args[]) { test a = new Jtest(); a.test1(); a.test2(); a.test2(); } }
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)
public void test() { System.out.println("Test 1"); //command line 1 System.out.println("Test 2"); //command line 2 }
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
You can pass a Runnable to a Thread.
Something like this will work
There are more elegant solutions using the reflection API.
Something like this will work
Java Syntax (Toggle Plain Text)
// rest of your code ... public static void main(String[] args) { test t = new test(); class R1 implements Runnable { test tt; public R1(test t) {tt=t;} public void run() {tt.test1();} } class R2 implements Runnable { test tt; public R2(test t) {tt=t;} public void run() {tt.test2();} } class R3 implements Runnable { test tt; public R3(test t) {tt=t;} public void run() {tt.test3();} } new Thread(new R1()).start(); new Thread(new R2()).start(); new Thread(new R3()).start(); }
There are more elegant solutions using the reflection API.
![]() |
Similar Threads
- I want google to index forum threads (Search Engine Optimization)
- new way the threads are timed (DaniWeb Community Feedback)
- IE won't let me access threads. Could someone email me a solution please? (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: Shifting Elements in an Array k places to the right.
- Next Thread: Database not being populated....
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






