Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Threads

Reply
Thread Tools

Threads

 
 
freesoft_2000
Guest
Posts: n/a
 
      02-07-2005
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

 
Reply With Quote
 
 
 
 
Sebastian Scheid
Guest
Posts: n/a
 
      02-07-2005

"freesoft_2000" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) lkaboutprogramming.com...
> 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();
Code:
I think you mean "new test()".

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

Regards
Sebastian


 
Reply With Quote
 
 
 
 
frankgerlach22@gmx.de
Guest
Posts: n/a
 
      02-11-2005
don't forget to join() the created thread at the end of the main
method. Otherwise you might not see anything...

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Threads - Get running threads Pedro Pinto Java 2 04-08-2008 11:44 PM
[new to threads] threads with UI and loop Une bévue Ruby 0 06-14-2006 10:22 AM
TB View, Threads, Threads with unread The Invisible Man Firefox 1 03-20-2006 02:09 AM
Standard Threads vs Weightless Threads yoda Python 2 08-01-2005 09:12 PM
threads without threads sindica@gmail.com C Programming 4 08-27-2004 09:25 PM



Advertisments