开发者

Java Multithreading Challenge [closed]

开发者 https://www.devze.com 2022-12-29 13:53 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. 开发者_如何学Python
Closed. This question needs to be more focused. It is not currently accepting answers.
开发者_如何学Python

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 5 years ago.

Improve this question

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) {}
    }
}


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().


A good place to start would be sun's website


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消