簡體   English   中英

如何讓線程在單個文件上協同工作?

[英]How can I have threads work together on a single file?

我有這個凱撒密碼,這是我在 1.5 年前作為一個項目制作的一個項目,我將重用它,因為我的任務是創建一個利用線程的程序。 這是我刪除的帖子,因為我未能提供適當的細節順便說一句。

這是我到目前為止的進展。

    //Enter the file name and/or path. 
    //Note: This is not case-sensitive but enter the file name and/or path correctly.
    System.out.print("Enter a file name: ");
    String filename = in.nextLine();
    
    //Enters the encryption key.
    System.out.print("Enter your key: ");
    int key = in.nextInt();
    


    File file = new File(filename);
    
    byte[] filebyte = new byte[(int) file.length()];
    
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(filebyte);
            
            thread1.start(filebyte);
            thread2.start(filebyte);
            thread3.start(filebyte);
            thread4.start(filebyte);
            
            //Then written back into the file.
            FileOutputStream newfile = new FileOutputStream(file);
            newfile.write(filebyte);
            newfile.close();
            System.out.println("File encrypted successfuly.");
        } 
        catch (FileNotFoundException e) {
            System.out.println("File Not Found.");
            e.printStackTrace();
        }

將執行加密的 4 個線程中的 2 個。 每個線程所做的是根據每個線程i開始的位置開始移動,然后跳轉 4 個字節並移動當前字節。 (對不起。我很難措辭。)

class Thread1 extends Thread {
    private int key;
    publlic Thread4(int enkey) {
        this.key = key;
    }
    public void run() {
        //The characters inside the array are shifted here.
        for (int i = 0; i < filebyte.length; i+=4) {
            filebyte[i] = (byte) ((filebyte[i] + key) % 256);
        }
    }
}

class Thread2 extends Thread {
    private int key;
    publlic Thread4(int enkey) {
        this.key = key;
    }
    public void run() {
        //The characters inside the array are shifted here.
        for (int i = 1; i < filebyte.length; i+=4) {
            filebyte[i] = (byte) ((filebyte[i] + key) % 256);
        }
    }
}

我的問題是我不知道如何讓這些線程直接在文件的文件filebyte上一起工作。 如果我嘗試讓線程在移位后返回filebyte ,最終會導致線程嘗試返回 4 個不同版本的filebyte

這是我的程序應該如何工作的順便說一句:

文本/文件:Hello World

thread1 - H   o   r   shifted I   p   s
thread2 -  e       l  shifted  f   !   m
thread3 -   l   W   d shifted   m   X   e
thread4 -    l   o    shifted    m   p

線程完成加密后:Ifmmp!Xpsme

這是一個示例,您可以使用並了解它是如何工作的。

class ObscureThread implements Runnable
{
    private byte[] bytes;
    private int key;
    private int index;
    private int increment;
    public Thread thread;

    public final ObscureThread(byte[] bytes, int key, int threadQuantity, int threadIndex) {
        this.bytes= bytes;
        this.key = key;
        increment = threadQuantity; //How many threads?
        index = threadIndex; //threadIndex must starts from 0.
        thread = new Thread(this); //Make sure to use "this" and NOT just Thread().
        thread.start(); //Start the thread. run() will be called.
    }
    public final void run() {
        for(int i=index; i<bytes.length; i+=increment) {
            bytes[i] = (byte)((bytes[i] + key) % 256);
        }
    }
}

public class Main
{
    public static void main(String[] args) throws Exception
    {
        byte[] bytes = new byte[] { 'H','e','l','l','o',' ','W','o','r','l','d' };
        int key = 1;
        int numOfThreads = 4; //Doesn't have to be 4.
        for(int i=0; i<numOfThreads; i++) {
            ObscureThread thread = new ObscureThread(bytes, key, numOfThreads, i);
            thread.thread.join(); //Wait for the threads to finish.
        }
        for(int i=0; i<bytes.length; i++) {
            System.out.print((char) bytes[i]);
        } //Ifmmp!Xpsme
        System.out.println();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM