views:

130

answers:

4

Can someone please explain or give an example on how to create multiple threads with different functionalities in Java?

This is the code... I am trying to create a thread that does what public void rnd_list() is supposed to do ( a list of 30 random numbers) which is a different task than the one define in public void run() which is the method that by default when thread.start() its executed. I want to know how to create a thread within the same class Apple with a different functionality in this case the one in public void rnd_list().

import java.util.Random;

public class Apple implements Runnable{

 String name;
 int time, number, first, last, maximum;
 Random r = new Random();
 Random n = new Random();
 int[] array = new int[10]; 


 public Apple(String s, int f, int l){
  name = s;
  first = f;
  last = l;
  maximum = array[0];
 }

// public void rnd_list()
// {
//   for(int j = 0; j < 30; j++)
//  {
//   number = n.nextInt(100);
//   array[j] = number;
//   System.out.println("thread" + name + "array [" + j + "] =" + array[j]);
//  }
// }

 public void run(){
  try{

   for(int i = first; i < last; i++ )
   {

    if(maximum < array[i])
    {
     maximum = array[i];
    }
   }

   System.out.println("maximum = " + maximum);
  }catch(Exception e){}
  }



 public static void main(String[] args){

  Thread t1 = new Thread(new Apple("one ", 0, 2));
  Thread t2 = new Thread(new Apple("two ", 3, 5 ));
  Thread t3 = new Thread(new Apple("three ", 6, 9));

  try{
   t1.start();
               t2.start();
               t3.start();


   }catch(Exception e){}

 }
}
+1  A: 

A good place to start would be sun's website

Guillaume
thanks, but I have already search there
KJP
Uhh... KJP, did you even click on that link?
RarrRarrRarr
I clicked on the link otherwise I wouldn't be able to know if I visited the link
KJP
The doc is well written an covers pretty much all you need to know. Your question is very vague, we'd be glad to explain if there's something you need to clear up.
Guillaume
thanks for the advice I'll put the code and my question will be updated
KJP
+2  A: 

When a Thread is created with a Runnable, the Runnable.run() method will be run when Thread.start() is called. If you want the rnd_list() method to be run, you have two options.

  1. Create a new class which implements Runnable and have the run() method in that class call rnd_list()
  2. Change the implementation of Apple.run() to check some state belonging to Apple and optionally execute rnd_list().
Tim Bender
A: 

Just to clarify: you want to run rnd_list() in a separate thread, and use run() for some other task?

It's not terribly good practice, but you could start a new thread on this specific method by adding this method to your Apple class

   Thread startRndList()
   {
      Thread t = new Thread(new Runnable() {
              public void run()
              {
                  rnd_list();
              }
        });
      t.start();
      return t;
   }

But rather than directly instantiating threads, you're better off using concurrency utils ExecutorService class.

mdma
A: 

I think you could use anonymous classes or nested classes that implement Runnable, but it would be nasty and you might have concurrency issues...

Thiago