p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java_objects thread: Re: Thread questions


Message #1 by Jacquie Barker <jjbarker@e...> on Wed, 05 Jun 2002 04:59:15 -0400
--------------5D7C06FDAEA886B1D567576F
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

When I compiled your program, all four threads did indeed run; the problem is that you are
probably looking for thread #3 to use the name that you are passing in.  See the change
that I've made to get the thread to print its name:

// Approach 1 : Extending the java.lang.Thread class
class SimpleThread extends Thread {
        public void run() {
                //? System.out.println( "SimpleThread running ... ");
                System.out.println( "SimpleThread named " + getName() + " is running ...
");
        }
}

// Approach 2 : Implementing the java.lang.Runnable interface.
class SimpleRunnable implements Runnable {
        public void run() {
                System.out.println( "SimpleRunnable running ... ");
        }
}

public class ThreadDemo {
        public static void main(String s[] ) {
                Thread t1 = new SimpleThread();
                Thread t2 = new Thread(new SimpleThread());
                Thread t3 = new Thread(new SimpleThread(), "My first thread");
                Runnable target = new SimpleRunnable();
                Thread t4 = new Thread(target);

                t1.start();
                t2.start();
                t3.start();
                t4.start();
        }
}

Even then, the name that you are passing in doesn't affect the SimpleThread instance,
because it is being given to the name of the enclosing Thread; you'd have to do the
following to achieve what I believe you were trying to accomplish (see bold):

// Approach 1 : Extending the java.lang.Thread class
class SimpleThread extends Thread {
 // Create a Constructor that will take a name as an argument.
 public SimpleThread(String name) {
  super(name);
 }

 // Also provide a "no args" Constructor.
 public SimpleThread() {
  super();
 }

        public void run() {
                //? System.out.println( "SimpleThread running ... ");
                System.out.println( "SimpleThread named " + getName() + " is running ...
");
        }
}

// Approach 2 : Implementing the java.lang.Runnable interface.
class SimpleRunnable implements Runnable {
        public void run() {
                System.out.println( "SimpleRunnable running ... ");
        }
}

public class ThreadDemo2 {
        public static void main(String s[] ) {
                Thread t1 = new SimpleThread();
                Thread t2 = new Thread(new SimpleThread());
                //? Thread t3 = new Thread(new SimpleThread(), "My first thread");
                Thread t3 = new Thread(new SimpleThread("My first thread"));
                Runnable target = new SimpleRunnable();
                Thread t4 = new Thread(target);

                t1.start();
                t2.start();
                t3.start();
                t4.start();
        }
}


mrizwan ullah wrote:

> how this programme is working.
> // Approach 1 : Extending the java.lang.Thread class
> class SimpleThread extends Thread
> {
>         public void run()
>         {
>                 System.out.println( "SimpleThread running ... ");
>                 // Include code here to get the work done by the
> thread.
>         }
> }
>
> // Approach 2 : Implementing the java.lang.Runnable
> interface.
> class SimpleRunnable implements Runnable
> {
>         public void run()
>         {
>                 System.out.println( "SimpleRunnable running ... ");
>                 // Include code here to get the work done by the
> thread.
>         }
> }
>
> public class ThreadDemo
> {
>         public static void main( String s[] )
>         {
>                 // No-arg constructor
>                 Thread t1 = new SimpleThread();
>
>                 // Using SimpleThread reference as a target
>                 Thread t2 = new Thread( new SimpleThread());
>
>                 // Using a name for the thread.
>                 Thread t3 = new Thread( new SimpleThread(), "My
> first thread");
>
> // Using SimpleRunnable as a target     //second
> example to implementsrunnable
>
>         // as under. @2
>                 Runnable target = new SimpleRunnable();
>                 Thread t4 = new Thread( target );
>
>                 t1.start();
>                 t2.start();
>                 t3.start();
>                 t4.start();
>         }
> }
> why thread 3 did not printed ?
>
> output
> SimpleThread running ... Thread-1
> SimpleThread running ... Thread-2
> SimpleThread running ... Thread-4
> SimpleRunnable running ...
> Press any key to continue . . .
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! - Official partner of 2002 FIFA World Cup
> http://fifaworldcup.yahoo.com
>



  Return to Index