Pipeline flow and wait / notify cross backup

Posted by dragin33 on Tue, 12 Nov 2019 00:14:37 +0100

I. inter thread communication through pipeline

1.1 pipeStream

A special stream used to transfer data directly between threads. One thread sends data to the output pipeline, and the other takes data from the input pipeline. By using pipes, communication between different threads can be realized without the aid of something like temporary files.

1.2 classes provided by JDK for thread communication

  • Pipedinputstream and PipedOutputStream
  • PipedReader and PipedWriter

1.3 byte stream reading

Reading data:

public class readData {
    public void readMehtod(PipedInputStream input) throws IOException {
        System.out.println("Read Here we go");
        byte[] byteArray = new byte[20];
        int flag = input.read(byteArray);
        while(flag != -1){
            String Data = new String(byteArray, 0, flag);
            System.out.println(Data);
            flag = input.read(byteArray);
        }
        input.close();
    }
}

Writing data:

public class writeData {
    public void writeMethod(PipedOutputStream out) throws IOException {
        System.out.println("write");
        for (int i = 0; i < 300; i++) {
            String outDate = " " + i;
            out.write(outDate.getBytes());
            System.out.println(outDate);
        }
        out.close();
    }

}

Read data thread:

public class readThread extends Thread {
    readData a;
    PipedInputStream in;
    public  readThread(readData a, PipedInputStream in){
        this.a = a;
        this.in = in;
    }

    @Override
    public void run() {
        try {
            a.readMehtod(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Write data thread:

public class writeThread extends Thread{
    writeData a;
    PipedOutputStream out;
    public writeThread(writeData a, PipedOutputStream out){
        this.a = a;
        this.out = out;
    }

    @Override
    public void run() {
        try {
            a.writeMethod(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Test method:

public class test {
    public static void main(String[] args) throws IOException {
        PipedInputStream in = new PipedInputStream();
        PipedOutputStream out = new PipedOutputStream();
        in.connect(out);
        readData a = new readData();
        writeData b = new writeData();
        writeThread write = new writeThread(b, out);
        readThread read = new readThread(a, in);
        write.start();
        read.start();
    }
}

1.4 character stream reading

Reading method:

public class readData {
    public void readMehtod(PipedReader input) throws IOException {
        System.out.println("Read Here we go");
        char[] byteArray = new char[20];
        int flag = input.read(byteArray);
        while(flag != -1){
            String Data = new String(byteArray, 0, flag);
            System.out.println(Data);
            flag = input.read(byteArray);
        }
        input.close();
    }
}

Writing method:

public class writeData {
    public void writeMethod(PipedWriter out) throws IOException {
        System.out.println("write");
        for (int i = 0; i < 300; i++) {
            String outDate = " " + i;
            out.write(outDate);
            System.out.println(outDate);
        }
        out.close();
    }
}

Only when the data is written in the pipeline can the read thread read the data, otherwise the read thread will block the in.read(xx) method.

1.5 difference between characters and bytes

https://www.cnblogs.com/mrjade/p/6861107.html

II. Wait / notify cross backup

2.1 problem description

Create 20 threads, 10 threads backup data to database A, 10 threads backup data to database B, and backup to database A and backup to database B are cross running.

2.2 solution

Class code:

package backup;

public class backup {
    volatile private boolean flag = false;
    synchronized public void backupA(){
        while(flag == false){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + "*****");
        }
        //A backup finished, B turn
        this.flag = false;
        this.notifyAll();

    }
    synchronized  public void backupB(){
        while(flag == true){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + "-----");
        }
        //B backup finished, it's A's turn
        flag = true;
        notifyAll();
    }
}

A backup thread:

public class backupA extends Thread {
    backup a;
    public backupA(backup a){
        this.a = a;
    }

    @Override
    public void run() {
        a.backupA();
    }
}

B backup thread

package backup;

public class backupB extends Thread{
    backup b;
    public backupB(backup b){
        this.b = b;
    }

    @Override
    public void run() {
        b.backupB();
    }
}

Run class:

public class test {
    public static void main(String[] args) {
        backup backup = new backup();
        for (int i = 0; i < 10; i++) {
            backupA a = new backupA(backup);
            a.start();
            backupB b = new backupB(backup);
            b.start();
        }
    }
}

Result:

Thread-1-----
Thread-1-----
Thread-1-----
Thread-1-----
Thread-1-----
Thread-0*****
Thread-0*****
Thread-0*****
Thread-0*****
Thread-0*****
.......

Topics: Database JDK