Java 我编写的ethod还检查run方法是否已完成运行。这太奇怪了@ca还提到调用线程应该休眠一段时间。你也可以测试一下。 CreateLogTable CLT = new CreateLogTable(); int numThreads = 2; int i

Java 我编写的ethod还检查run方法是否已完成运行。这太奇怪了@ca还提到调用线程应该休眠一段时间。你也可以测试一下。 CreateLogTable CLT = new CreateLogTable(); int numThreads = 2; int i,java,multithreading,Java,Multithreading,我编写的ethod还检查run方法是否已完成运行。这太奇怪了@ca还提到调用线程应该休眠一段时间。你也可以测试一下。 CreateLogTable CLT = new CreateLogTable(); int numThreads = 2; int index = 0; DownloadFileThread[] dlThreads = new DownloadFileThread[numThreads]; for (S3ObjectSummary oSummary : bucketKeys.


我编写的ethod还检查run方法是否已完成运行。这太奇怪了@ca还提到调用线程应该休眠一段时间。你也可以测试一下。
CreateLogTable CLT = new CreateLogTable();

int numThreads = 2;
int index = 0;
DownloadFileThread[] dlThreads = new DownloadFileThread[numThreads];
for (S3ObjectSummary oSummary : bucketKeys.getObjectSummaries()) {
    while (dlThreads[index] != null && dlThreads[index].isAlive()) {
        index += 1;
        index = index % numThreads;
    }
    dlThreads[index] = new DownloadFileThread(CLT , getBucket(oSummary.getBucketName() + "/"
                    + oSummary.getKey()), getFile(oSummary.getKey()), index);
    dlThreads[index].start();
    index += 1;
    index = index % numThreads;
}
try {
    System.out.println("Creating thread " + this.threadnum);
    this.fileObj = this.S3CLIENT.getObject(new GetObjectRequest(this.filePath, this.fileName));
    this.fileIn = new Scanner(new GZIPInputStream(this.fileObj.getObjectContent()));
    while (this.fileIn.hasNext()) {         
        this.parent.forwardToTable(fileIn.nextLine());
    }
    System.out.println("Finished " + this.threadnum);
} catch (Throwable e) {
    System.out.println("Downloading of " + this.fileName + " failed.");
}
Creating thread 0
Creating thread 1
Creating thread 0
Creating thread 1
Creating thread 0
Creating thread 1
Creating thread 0
...
Creating thread 1
Creating thread 0
Creating thread 1
Finished 0
Finished 1
Finished 1
Finished 0
Finished 1
Finished 1
...
Finished 0
Finished 1
Finished 0
Finished 1
public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            Runnable worker = new WorkerThread("" + i);
            executor.execute(worker);
          }
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        System.out.println("Finished all threads");
}
public class ThreadTest {
    private static class SleepThread extends Thread {
        private final int index;
        SleepThread(int ii) { index = ii; }

        @Override
        public void run() {
            System.out.println("Creating thread " + this.index);
            try {
                Thread.sleep(5_000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Finished " + this.index);
        }
    }

    public static void main(String[] args) {
        int numThreads = 2;
        int index = 0;
        SleepThread[] dlThreads = new SleepThread[numThreads];
        for (int ii = 0; ii < 10; ++ii) {
            while (dlThreads[index] != null && dlThreads[index].isAlive()) {
                index += 1;
                index = index % numThreads;
            }
            dlThreads[index] = new SleepThread(index);
            dlThreads[index].start();
            index += 1;
            index = index % numThreads;
        }
    }
}
//this.isWorking initialized to true during instantiation

@Override
public void run() {
    try {
        System.out.println("Creating thread " + this.threadnum + " for " + filePath + "/" + fileName);
        this.fileObj = this.S3CLIENT.getObject(new GetObjectRequest(this.filePath, this.fileName));
        this.fileIn = new Scanner(new GZIPInputStream(this.fileObj.getObjectContent()));
        while (this.fileIn.hasNext()) {
            this.parent.forwardToTable(fileIn.nextLine());
        }
        System.out.println("Finished " + this.threadnum);
        this.isWorking = false;
    } catch (Throwable e) {
        System.out.println("Downloading of " + this.fileName + " failed.");
        e.printStackTrace();
        this.isWorking = false;
    }
}

public boolean isWorking(){
    return this.isWorking;
}