提交 cd729752 编写于 作者: X xiaoyu

add thread stream

上级 aaa61e68
package com.example.demo.controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/**
* 数据流
*/
@RestController
@RequestMapping("/")
public class StreamController {
/**
* 字符数据流
*/
@GetMapping("/character")
public void characterDemo() throws IOException {
//CharArrayWriter用来接收数据
CharArrayWriter charArrayWriter = new CharArrayWriter();
charArrayWriter.write("cat1"); //可以是字符串或者下面的字节数组
char[] char01 = {'c','a','t','2'};
charArrayWriter.write(char01);
char[] char02 = charArrayWriter.toCharArray(); //转字符数组
System.out.print(char02);
System.out.print("\r\n");
CharArrayReader charArrayReader = new CharArrayReader(char02);
CharArrayReader reader = new CharArrayReader(char02);
while (charArrayReader.read() != -1){ //读数据
System.out.print((char) reader.read());
}
System.out.print("\r\n");
//推荐使用
BufferedReader bufferedReader = new BufferedReader(new CharArrayReader(char02));
System.out.print(bufferedReader.readLine());
}
/**
* 字节数据流&文件流
*/
@PostMapping("/byte")
public void byteDemo(@RequestPart @RequestParam("file") MultipartFile file) throws IOException {
String filename = file.getOriginalFilename(); //获取文件名
byte[] fileContent = file.getBytes();
FileOutputStream fileOutputStream = new FileOutputStream("test.txt");
fileOutputStream.write(fileContent); //写入
fileOutputStream.close(); //关闭
File file2 = new File("test2.txt");
File file2rename = new File("test2rename.txt");
file2.renameTo(file2rename);
if(file2.createNewFile() == false){
System.out.print("文件新建失败");
}
file2.delete(); //删除
}
}
package com.example.demo.controller;
import com.example.demo.tool.Task;
import com.example.demo.tool.TaskRunnable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 多线程
*/
@RestController
@RequestMapping("/")
public class ThreadController {
@GetMapping("/thread")
public void threadDemo(){
Task thread1 = new Task();
Task thread2 = new Task();
Task thread3 = new Task();
thread1.setName("task1");
thread2.setName("task2");
thread3.setName("task3");
thread1.start();
thread2.start();
thread3.start();
}
@GetMapping("/runnable")
public void runnableDemo(){
TaskRunnable taskRunnable = new TaskRunnable();
Thread thread1 = new Thread(taskRunnable,"task1");
Thread thread2 = new Thread(taskRunnable,"task2");
Thread thread3 = new Thread(taskRunnable,"task3");
thread1.start();
thread2.start();
thread3.start();
}
}
package com.example.demo.tool;
import lombok.SneakyThrows;
public class Task extends Thread {
int count = 0;
@SneakyThrows
@Override
public void run() {
while (true){
if(count > 5){
break;
}
System.out.print(this.getName() + ":" + String.valueOf(count) + "\r\n");
count ++;
Thread.sleep(500);
}
super.run();
}
}
package com.example.demo.tool;
import lombok.SneakyThrows;
/**
* 实现了Runnable接口
*/
public class TaskRunnable implements Runnable {
int count = 0;
@SneakyThrows
@Override
public synchronized void run() {
while (true){
if(count > 5){
break;
}
System.out.print(String.valueOf(count) + "\r\n");
count ++;
wait(500);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册