Solved

Thread question...

Posted on 2004-09-02
8
236 Views
Last Modified: 2010-03-31
I asked this question last night...

http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/Java/Q_21116212.html

I want to be able to spawn a thread that spawns different class as well as my original.  Can I overload any of the methods?  How exactly do I spawn a thread that can run either one class, or another?
0
Comment
Question by:iismatt
8 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11962689
Not sure what you are asking (couldn't infer anything from previous question...) but here's a go...

// objA is one runnable to run, objB is another runnable to run...
new Thread( new Runnable() { public void run() { objA.run(); objB.run(); } } ).start(); // runs objA and objB consequetively in a seperate thread.

// start objA and objB (both implement java.lang.Runnable) in seperate threads.
new Thread( objA ).start();
new Thread( objB ).start();
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11962718
or you could do something like the following...

public class Test extends Thread {
    public Test( Runnable runnable ) {
        super( runnable );
    }
    public void run() {
        super.run();
        System.out.println( "Test's runnable..." );
    }
    public static void main( String[] args ) {
        new Test( new Runnable() { public void run() { System.out.println( "Runnable as argument" ); } } ).start();
    }
}
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11962913
Do you want to start different threads from a thread?
if yes..
make other classes runnable as well and start them from the main thread
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 35

Expert Comment

by:TimYates
ID: 11962919
Just make class A and class B implement Runnable, and give them both "run" methods

public class A implements Runnable
{
    public void run()
    {
        System.out.println( "A" ) ;
    }
}

public class B implements Runnable
{
    public void run()
    {
        System.out.println( "B" ) ;
    }
}

Then, just do:

A a = new A() ;
Thread t = new Thread( a ) ;
t.start() ;

or

B b = new B() ;
Thread t = new Thread( b ) ;
t.start() ;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11962922
bah...too slow... ;-)
0
 
LVL 1

Author Comment

by:iismatt
ID: 11963051
Here, let me clarify exactly what I'm confused about ... i have...

   public void run() {
         movingRandRect mrr = new movingRandRect();
    }

    void runProg() {
         if (t_movement==null); {
              Thread t_movement = new Thread(this);
              t_movement.start();
         }
    }

On some instances, I want to initiate my movingTriange.class instead of movingRandRect.  How would I do that?
0
 
LVL 35

Accepted Solution

by:
girionis earned 100 total points
ID: 11963131
Have a switch or something and do something like:

public boolean triangle = false;

...
...
triangle = true;

public void run() {
         if (triangle)
         {
             // instatiate movingTriangle
             triangle = false;
         }
         else
         {
                movingRandRect mrr = new movingRandRect();
         }
    }
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11963138
Create an interface for your classes you want to pass to the thread...

public interface MyShape
{
    public void doStuff() ;
}

implement MyShape in movingRandRect and movingTriange classes, then write a Thread class like this:

public class Mover extends Thread
{
    MyShape shape = new MyShape() ;
    public Mover( MyShape shape )
    {
        this.shape = shape ;
    }

    public void run()
    {
        shape.doStuff() ;
    }
}

Then, you can do:

    void runProg() {
         if (t_movement==null);
         {
              Mover t_movement = new Mover( new movingRandRect() );
              t_movement.start();
         }
    }

or

    void runProg() {
         if (t_movement==null);
         {
              Mover t_movement = new Mover( new movingTriange() );
              t_movement.start();
         }
    }
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question