我怎样才能使用多线程?

时间:2014-03-29 06:36:14

标签: java multithreading

你能帮助我multithreading,例如我如何让这个方法在两个线程中运行。

/**
     * returns 16 bytes
     */
    private byte[] keyStream() {
        nextState();
        final byte[] s = new byte[16];
        /* unroll */
        int x = X[6] ^ X[3] >>> 16 ^ X[1] << 16;
        s[0] = (byte) (x >>> 24);
        s[1] = (byte) (x >> 16);
        s[2] = (byte) (x >> 8);
        s[3] = (byte) x;
        x = X[4] ^ X[1] >>> 16 ^ X[7] << 16;
        s[4] = (byte) (x >>> 24);
        s[5] = (byte) (x >> 16);
        s[6] = (byte) (x >> 8);
        s[7] = (byte) x;
        x = X[2] ^ X[7] >>> 16 ^ X[5] << 16;
        s[8] = (byte) (x >>> 24);
        s[9] = (byte) (x >> 16);
        s[10] = (byte) (x >> 8);
        s[11] = (byte) x;
        x = X[0] ^ X[5] >>> 16 ^ X[3] << 16;
        s[12] = (byte) (x >>> 24);
        s[13] = (byte) (x >> 16);
        s[14] = (byte) (x >> 8);
        s[15] = (byte) x;
        return s;
    }

1 个答案:

答案 0 :(得分:2)

您可以简单地创建一个类,实现Runnable并覆盖run函数,如下所示:

class MyRunnable implements Runnable{
    public void run(){
        byte[] retVal = keyStream();
    }
    /**
     * returns 16 bytes
     */
    private byte[] keyStream() {
        // function implementation
    }
}

您可以通过某种方式使用它,例如:

Thread myThread1= new Thread(new MyRunnable());
Thread myThread2= new Thread(new MyRunnable());
myThread1.start();
myThread2.start();