public class ReadData {
public void readMethod(PipedInputStream input){
try{
System.out.print("read :");
byte[] bytes = new byte[20];
int length = input.read(bytes);
while(length != -1){
String newData = new String(bytes,0,length);
System.out.print(newData);
length = input.read(bytes);
}
System.out.println();
input.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
写入线程:
public class ThreadWrite extends Thread{
private WriterData write;
private PipedOutputStream out;
public ThreadWrite(WriterData write,PipedOutputStream out){
this.write = write;
this.out = out;
}
public void run(){
write.writeMethod(out);
}
}
读取线程:
public class ThreadRead extends Thread{
private ReadData read;
private PipedInputStream input;
public ThreadRead(ReadData read,PipedInputStream input){
this.read = read;
this.input = input;
}
public void run(){
read.readMethod(input);
}
public static void main(String[] args) {
try{
WriterData writeData = new WriterData();
ReadData readData = new ReadData();
PipedInputStream inputStream = new PipedInputStream();
PipedOutputStream outputStream = new PipedOutputStream();
inputStream.connect(outputStream);
ThreadRead threadread = new ThreadRead(readData,inputStream);
threadread.start();
Thread.sleep(2000);
ThreadWrite threadwrite = new ThreadWrite(writeData,outputStream);
threadwrite.start();
}catch(IOException e){
e.printStackTrace();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
利用变量控制线程的顺序
用于操作a和b的类
public class DBTools {
volatile private boolean preIsA = false;
synchronized public void backupA(){
try{
while(preIsA == true){
wait();
}
for(int i = 0;i < 5;i++){
System.out.println("*********");
}
preIsA = true;
notifyAll();
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized public void backupB(){
try{
while(preIsA == false){
wait();
}
for(int i = 0;i < 5;i++){
System.out.println(".........");
}
preIsA = false;
notifyAll();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
线程a:
public class BackupA extends Thread{
private DBTools dbtools;
public BackupA(DBTools dbtools){
this.dbtools = dbtools;
}
public void run(){
dbtools.backupA();
}
}
线程b:
public class BackupB extends Thread{
private DBTools dbtools;
public BackupB(DBTools dbtools){
this.dbtools = dbtools;
}
public void run(){
现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。
分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习