For your new testing codes, 2 things:
You may not understand multi-threading very well. They way you output the result can't show anything. You should output the result with the thread Id instead of putting it at the beginning:
public void run() { for (String s : lis) { System.out.println(Thread.currentThread().getId() + " : " + s); } }
Why do you put the list as
static
in theTestThreads
class? You may be better to check the way of usingstatic
. And I believe this is the reason. You can try my code which is just the same with yours except for removing thestatic
:import java.math.RoundingMode; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.google.common.collect.Lists; import com.google.common.math.IntMath; public class Test { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub int numOfthreads = 2; List<String> originalList = new ArrayList<String>(); for (int i = 0; i < 20; i++) { originalList.add(Integer.toString(i)); } int partitionSize = IntMath.divide(originalList.size(), numOfthreads, RoundingMode.UP); List<List<String>> partitions = Lists.partition(originalList, partitionSize); List<Callable<Object>> todo = new ArrayList<Callable<Object>>(); int count = 0; ExecutorService taskExecutor = Executors .newFixedThreadPool(numOfthreads); for (int i = 0; i < partitions.size(); i++) { TestThreads it = new TestThreads(partitions.get(i)); todo.add(Executors.callable(it)); System.out.println("Created thread " + count + ", containing: " + partitions.get(i).size() + " files\n"); for (String s : partitions.get(i)) { System.out.print(s + " "); } count++; System.out.println("\n"); } taskExecutor.invokeAll(todo); } public static class TestThreads implements Runnable { private List<String> lis; public TestThreads(List<String> list) { lis = list; } public void run() { for (String s : lis) { System.out.println(Thread.currentThread().getId() + " : " + s); } } } }
Update:
it was STATIC list that causes issue, everything works fine after removing it. Appreciate the time and help from Tim and Xiezi !