-1
import java.util.*;

public class test{


public test(){
     int[] list = new int[]{1,2,3,4,5,6}; 
     ArrayList<Integer> myList  = new Arrays.asList(list);
     //list with 6 numbers that require 3 threads           
  }

public static void main(String[] args){
     new test();



 }


}

For every two numbers in my list, i want to have a thread. i.e if their are 6 numbers, 3 threads should be detedted automatically. What must i do to automatically create Threads?

  • 1
    What about a simple loop that creates Threads? I don't understand what problem you're facing here. – Amongalen Mar 16 '20 at 13:14
  • What are these Threads supposed to be doing? – ControlAltDel Mar 16 '20 at 13:14
  • Does this answer your question? [Java Thread: start() - How does it create a new thread?](https://stackoverflow.com/questions/21357481/java-thread-start-how-does-it-create-a-new-thread) – Petterson Mar 16 '20 at 13:15
  • How do six values require three threads? What is each thread to do with its' two values? – Elliott Frisch Mar 16 '20 at 13:16
  • This was just a simple list ive made to demonstarate my problem. But in the real file, i have 6 game player instances in the list. And two of them battle each other with one thread. But if i want to add 2 more players, making it 8, i would have to manually type antoher thread.start() for it. Is there a way to automate the production of threads for every 2 players in the list? – The Lame Guy Mar 16 '20 at 13:20
  • As I said, a simple loop and you're good to go. However, if you don't get the basics of Java I'd leave the multithreading for later and focus on basics for now. – Amongalen Mar 16 '20 at 13:26
  • How is this a question about threads? What if, for every two numbers in your list you wanted to have a `File` or a `StringBuffer` or an object of some type that you defined. Would you know how to do that? – Solomon Slow Mar 16 '20 at 14:11

3 Answers3

1

Yes. The generally accepted method is to generate a Runnable and submit that Runnable to an ExecutorService to manage the threads for you. DON'T JUST EXECUTE A LOOSE THREAD. That's a terrible habit to get in and it makes it impossible for you to control the threads.

ExecutorService service = Executors.newFixedThreadPool(5);
for (blah blahblah) {
    Runnable runnable = new Runnable() {
    ....
    }
    service.execute(runnable);
}

Just be sure that when you're done with the executor (or at program termination), you do a

service.shutdown();

or

service.shutdownNow();
Ryan
  • 1,762
  • 6
  • 11
  • There is nothing wrong with manually creating Threads. In fact, doing so has at least one significant advantage over an ExecutorService. – VGR Mar 16 '20 at 14:10
  • I emphatically disagree. Firing off new Thread() allows for zero management or control of threads. You can get away with it in small applications, but anything larger and you're asking for trouble. It's a bad habit and should be avoided. – Ryan Mar 16 '20 at 14:36
  • You know you can store Threads in variables and fields, right? I do it all the time. For example, `private Thread backgroundTask;`. Or are you talking about something like a thread pool? I certainly wouldn’t try to implement my own thread pool. – VGR Mar 16 '20 at 14:38
0

Maybe you can add a judgment wheather container's size is a multiple of 2 after new Arrays.asList(list); or every time you add to this container. If it's not a multiple of 2 and it's not less than 2, then you can perform -1, and put the previous players who can form a pair in a thread. If there are other statements that operate on the list, you can also follow this logic.

int[] list = new int[]{1,2,3,4,5,6}; 
    ArrayList<Integer> myList  = new Arrays.asList(list);
    //list with 6 numbers that require 3 threads           
    if(myList.size() % 2 == 0){//if it is a multiple of 2
        for(int i=0;i<myList.size()/2;i++){
            //your code
            new Thread(new YourThread()).start();
        }
    }else{//if it isn't a multiple of 2
        for(int i=0;i<(myList.size()-1)/2 || myList.size()>2;i++){
            //e.g: myList.size()=7 7-1=6 6/2=3

            //your code
            new Thread(new YourThread()).start();
        }
    }
VioletTec
  • 71
  • 5
0

import java.util.*;

public class test{

public void test(){
    int[] list = new int[]{1,2,3,4,5,6}; 
    List<Thread> threadList = new ArrayList<Thread>();
    for(int i =0; i < list.length/2; i++) {
        Thread thread = new Thread();
        threadList.add(thread);
    }         
}

public static void main(String[] args){
    new test();
}

}