Solved

Thread question...

Posted on 2004-09-02
8
233 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 35

Expert Comment

by:TimYates
Comment Utility
bah...too slow... ;-)
0
 
LVL 1

Author Comment

by:iismatt
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IT Company 5 68
countPairs challenge 7 57
JDeveloper 12c for 32 bit 4 34
maven project error 5 17
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now