简体   繁体   中英

How to run x amount of threads in java and join them together?

I am trying to just mess around with some parallelism for a larger project, and I want to figure out how to run whatever amount of threads the user inputs. My program hangs on the join call when I try to add everything together. I also was wondering if there is a way to directly call data, since I want to be able to compare data in each thread, however, for now I am just trying to run multiple.

import java.util.Scanner;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Task extends Thread {

    static int seatCount = 0;
    static Lock taskLock = new ReentrantLock();

private void addSeats() {
    taskLock.lock();
    seatCount++;
    taskLock.unlock();
}
public void run() {
    for (int i=0; i<1000000000; i++)
       addSeats();

}

}

public class ThreadManager {
    public static void main(String[] args) throws InterruptedException {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input the number of Threads you want to create: ");
        int n = input.nextInt();
        System.out.println("You selected " + n + " Threads");

        Thread[] threads = new Thread[n];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Task();
            threads[i].start();
        }

        for (int j = 0; j < n; j++) {
            threads[j].join();
        }

        System.out.println("There are " + Task.seatCount + " seats");

    }
}

well as i checked its not handing just take a lot of time You can try:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Task extends Thread {

    static long seatCount = 0;
    static Lock taskLock = new ReentrantLock();


    public void run() {
        for (int j=0; j<10; j++){
            for (int i=0; i<100000000; i++){
                taskLock.lock();
                    seatCount++;
                taskLock.unlock();
            }
            System.out.print(".");
        }
    }
}

public class ThreadManager {
    public static void main(String[] args) throws InterruptedException {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input the number of Threads you want to create: ");
        int n = input.nextInt();
        System.out.println("You selected " + n + " Threads");

        List<Thread> threads = new ArrayList<>(n);

        for (int i = 0; i < n; i++) {
            Task e = new Task();
            threads.add(e);
            e.start();
        }

        for (Thread thread : threads) {
            thread.join();
        }

        System.out.println("There are " + Task.seatCount + " seats");

    }
}

As You will noticed the dots will appear but slowly. I also reformat your code a little bit I dont quiet get what You mean by call data directly

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