Threads

F

freesoft_2000

Hi everyone,

I have a question about threads. Consider the below
program

Code:
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

Code:
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
 
S

Sebastian Scheid

freesoft_2000 said:
Hi everyone,

I have a question about threads. Consider the below
program

Code:
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();[/QUOTE]

I think you mean "new test()".
[QUOTE]
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

Code:
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

Try this:
---------
Runnable runner = new Runnable() {
public void run() {
// put your commands here
}
};
Thread t = new Thread(runner);
t.start(); // executes the runners run()-method in a separate thread.
Returns immediately
 
F

frankgerlach22

don't forget to join() the created thread at the end of the main
method. Otherwise you might not see anything...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Implementing Many Stacks in the Same Program 1
Help in hangman game 1
Error with server 3
Void problem 1
Java method query 2
School Project 1
Java problem 1
THREADS - SOCKETS 2

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top