简体   繁体   中英

How do I use a method with parameter in Threads?

I want to use a Thread that sorts a list with my method quicksort.

Unfortunately I dont know how to do that.

   ------------------
blabla other Methodes that quicksort/ sort needs....

   ------------------




public static void sort(int[] list) {
        quickSort(list, 0, (list.length-1)); // <--- I want use this in a Thread

    }  


 public static void main (String[] args) {
        int[] newList = {5, 7, 23, 87, 11, 0, 5, 33, 30,512, 125, 215 ,7, 234, 152, 125};
        for(int i: newList) {
            System.out.print(i + " ");
        }

        System.out.println();
        sort(newList);                // <--- OR I want use this in a Thread

My Problem is that I dont know how to use a Method with Parameters in a Thread.

Does someone know a good Tutorial that explains me how to do that?

What better implement Runnable or extends Thread?

    final int[] newList = {5, 7, 23, 87, 11, 0, 5, 33, 30,512, 125, 215 ,7, 234, 152, 125};

    System.out.println();

    thread t = new Thread () {
        public void run () {
            sort(newList);     
        }
    };
    t.start();

Note that 'newList' has to be final.

You've got one basic approach and a couple implementations. The basic approach is to extend the base class and pass in your parameter to the constructor. Store that parameter in a final field, and then access it from your run() method. Example:

public class QuickSort extends Thread {
    final int[] entries;

    public QuickSort(int[] list) {
        entries = list;
    }

    public void run() {
        // do your quick sort.
        // the final int[] entries just keeps the list from being
        // replaced at run time, the entries themselves are not frozen.
    }
}

To use the thread, you would execute it like this:

QuikSort qs = new QuickSort(myList);
qs.start();

// When you need to wait for the sort to be done:
// Don't try to use the list before hand because it's
// state will be undefined.
qs.join();

The problem with this is we are burning a whole thread for a relatively short time. Threads are expensive operating system resources and they can take a while to finish cleaning up. It's better to reuse threads. Thankfully, the Executor system was created to make this easier (I remember creating one in a framework a long time ago before this was available).

The end strategy is effectively the same, except your Executor is in charge of spinning up or reusing threads for your short term asynchronous work.

You can really only do this by using fields to store the data.

You can easily accomplish this with a constructor;

public Worker extends Thread
{
   Object data1, data2, //...
   public Worker(Object data0, Object data1, ...)
   {
    //set the fields
   }

   public void run()
   {
      //do stuff using the fields we set
   }
}

Now to pass the thread the correct data; new Worker(data1, data2, ...).start();

But thread creation is very expensive, and you want to make each thread do as much as possible. For example, if you needed to run this method a significant number of times, then the Executor framework, or having your threads pull data from collections, would be more appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM