在Java中启动多个线程

时间:2014-10-13 15:50:25

标签: java multithreading

您好我的练习中需要显示多线程的资源访问问题。

我需要增加共享(线程之间)int表的索引,该表名为histogramTable[],表的大小已知,数据在文件中。每个线程都有自己的范围,称为interval。例如:我有4个线程,每个线程获得以下索引:

线程1:0 - 1_000_000
线程2:1_000_000 - 2_000_000
主题3:2_000_000 - 3_000_000
主题4:3_000_000 - 4_000_000

这是我的问题,一旦你在线程上启动一个给定数字的程序,它的threadsNumber变量,似乎只有一个线程正在运行。因为字节总和总是tabSize / threadsNumber。对于上面的示例,它是1_000_000字节。

对于线程访问问题,它应该是3_800_000 - 4_000_000字节。你能告诉我我做错了吗?

我给你的全部代码在我看来是短暂的。还有一个注释掉的函数randomizeBytes()来快速生成字节文件。

Ex.java

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Ex2 {

    private static int threadsNumber = 4, tabSize = 4000000;
    public static int threadsCounter;
    public static byte[] dataTab = loadBytes();

    public static byte[] loadBytes() {

        byte data[] = new byte[tabSize];
        Path path = Paths.get("dane.txt");
        try {
            data = Files.readAllBytes(path);
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    /*private byte[] randomizeBytes() {

        Path path = Paths.get("binaryData.txt");
        byte bytes[] = new byte[tabSize];
        new Random().nextBytes(bytes);
        try {
            Files.write(path, bytes);
        } catch (IOException e) { 
            e.printStackTrace();
        }

        return bytes;
    }*/

    public static void runThreads(){
        threadsCounter = threadsNumber;
        int interval = tabSize / threadsNumber;
        int endIndex = 0;

        Thread[] threads = new Thread[threadsNumber];
        MyThread w;

        for(int i = 0 ; i < threadsNumber  ; i ++){
            endIndex = (i + 1) * interval;
            if(endIndex >= tabSize) 
                endIndex = tabSize;
            w = new MyThread(interval * i , endIndex);
            threads[i] = new Thread(w);
            threads[i].start();
            if(threads[i].isAlive())
                System.out.println("Thread number: " + i + " started and alive, indexes: " + interval*i + " - " + endIndex );
        }    
    }

    public synchronized static int decrementThreads(){
        return --threadsCounter;
    }

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

MyTherad.java

public class MyThread implements Runnable{

    private byte table[] = Ex2.dataTab;
    int startIndex,endIndex,temp;
    private int histogramTable[] = new int[256] ;
    private long timeStart, timeStop;

    public MyThread(int startIndex, int endIndex){
        this.startIndex = startIndex;
        this.endIndex = endIndex;
    }

    @Override
    public void run() { 
        timeStart = System.currentTimeMillis();
        for(int i = startIndex ; i < endIndex ; i ++) {
            temp = Byte.toUnsignedInt(table[i]);
            histogramTable[temp]++;
        }           
        timeStop = System.currentTimeMillis();
        System.out.println("Threads working: " + Ex2.threadsCounter);
        if(Ex2.decrementThreads() == 0) printSummary();
    }

    public void printSummary() {
        int sum = 0;
        for(int i : histogramTable) System.out.print(i + " ");
        System.out.println();
        for(int i = 0 ; i < 256 ; i ++)
            sum += histogramTable[i];
        System.out.println("Bytes: " + sum);
        System.out.println("Task complete in: " +  (timeStop - timeStart) + "ms");
    }
}

1 个答案:

答案 0 :(得分:0)

查看Thread.join()。请查看Java Multithreading concept and join() method答案中的解释。