Solved

Thread question...

Posted on 2004-09-02
8
237 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
Windows 10 IE Certificate Issue 10 51
jsp login check 12 43
JAVA API design with micro service cloud in mind 1 49
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …

820 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