工作一直在使用,今天把线程这块理一理
线程-线程池-netty
1,线程、进程、多线程
进程=程序写出来后执行,
线程=进程中含有多个线程,main,gc回收等
2、三种线程创建方式
继承Thread类
并行运行,主线程和子线程是同时运行的
实现Runnable接口
实现Callable接口
3、静态代理模式
这块可以自己网上找找
4、Lamdba表达式
public class LambdaDemo {
public static void main(String[] args) {
//2.实现静态内部类
static class Addition implements MathOperation{
@Override
public void operation(int a, int b) {
System.out.println(a + b);
}
}
//2.实现局部内部类
class Addition implements MathOperation{
@Override
public void operation(int a, int b) {
System.out.println(a + b);
}
}
//2.实现匿名内部类,实现接口,直接执行计算
new MathOperation(){
@Override
public void operation(int a, int b) {
System.out.println(a + b);
}
}.operation(1, 2);
}
//2.实现匿名内部类,实现接口
MathOperation addition = new MathOperation() {
@Override
public void operation(int a, int b) {
System.out.println(a + b);
}
};
addition.operation(1,2);
}
//2.Lambda简化,只有一块语句,省略接口和方法,只留下语句实现
MathOperation addition = (int a , int b) -> {
System.out.println(a + b);
};
//3.执行计算
addition.operation(1, 2);
}
//3.执行计算
MathOperation addition = new Addition();
addition.operation(1,2);
}
}
//1.定义一个函数式接口方法
interface MathOperation{
void operation(int a, int b);
}
//2.定义实现类
class Addition implements MathOperation{
@Override
public void operation(int a, int b) {
System.out.println(a + b);
}
}
Lamdba函数直接省略接口和方法名,()代替,里面可传参
5、线程状态
停止线程 - 设置boolean判断,写一个stop()方法即可
线程休眠 - sleep是不会释放锁
线程礼让 - 调用yield()方法
线程强制执行 - join
线程优先级
线程同步
死锁
6、生产者,消费者模式
这一块工作中是需要搭配到中间件来使用,目前用到的 kafka、emqx等
7、线程池--可以看看netty
8.io读写方法
io流里面的BufferedReader,BufferedWriter,FileInputStream,FileOutputStream,PrintWriter的基本读写方法总结了一趟
package com.lh.iostream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class iostream_text {
/*
* 用打印流PrintWriter來写入数据
*
* */
public void printstream(StringBuffer str) throws IOException
{
String content = String.valueOf(str);
File file = new File("src/lab02.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
PrintWriter prw = new PrintWriter(fw);
long start = System.currentTimeMillis();
prw.write(content);
prw.flush(); // 因为数据读取的时候,一般都是先将数据读取到内存里面,然后再是写入到文本里面的,
// 当数据读取完毕的时候并不意味着内容就完全写入到了文本里面了
// 此时文本内容还有可能会留在了缓存区里面,因此我们需要对缓存里面的数据进行flush();
System.out.println("---------printwriter is ok!---------");
long end = System.currentTimeMillis();
long total = end - start;
System.out.println("the total time is:" + total + "ms");
prw.close();
fw.close();
}
/*
* 用BufferedReader来写入数据
*
* */
public void bufferedwriter(StringBuffer str) throws IOException
{
String content = String.valueOf(str);
File file = new File("src/lab03.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bfw = new BufferedWriter(fw);
long start = System.currentTimeMillis();
bfw.write(content);
bfw.flush(); // 因为数据读取的时候,一般都是先将数据读取到内存里面,然后再是写入到文本里面的,
// 当数据读取完毕的时候并不意味着内容就完全写入到了文本里面了
// 此时文本内容还有可能会留在了缓存区里面,因此我们需要对缓存里面的数据进行flush();
System.out.println("---------BufferedReader is ok!---------");
long end = System.currentTimeMillis();
long total = end - start;
System.out.println("the total time is:" + total + "ms");
bfw.close();
fw.close();
}
/*
* 用打印流fileoutputwriter來写入数据
*
* */
public void fileoutputwriter(StringBuffer str) throws IOException
{
String content = String.valueOf(str);
File file = new File("src/lab04.txt");
if (!file.exists()) {
file.createNewFile();
}
byte Contentbyte[]=content.getBytes();
FileOutputStream out=new FileOutputStream(file);
long start = System.currentTimeMillis();
out.write(Contentbyte,0,Contentbyte.length);
System.out.println("----------FileOutputStream is ok!---------");
long end = System.currentTimeMillis();
long total = end - start;
System.out.println("the total time is:" + total + "ms");
out.close();
}
public void bufferedreader(File file) throws IOException
{
BufferedReader bfr=new BufferedReader(new FileReader(file));
String content=null;
long start = System.currentTimeMillis();
while(bfr.readLine() != null)
{
content=content+(bfr.readLine()+'\n');
}
System.out.println("----------BufferedReader is ok!---------");
long end = System.currentTimeMillis();
long total = end - start;
System.out.println("the total time is:" + total + "ms");
bfr.close();
}
public void fileinputstream(File file) throws IOException
{
FileInputStream in=new FileInputStream(file);
long start = System.currentTimeMillis();
byte Content_byte[]=new byte[(int) file.length()];
in.read(Content_byte);
System.out.println("----------fileinputstream is ok!---------");
long end = System.currentTimeMillis();
long total = end - start;
System.out.println("the total time is:" + total + "ms");
in.close();
}
public static void main(String[] sda) throws IOException {
iostream_text io=new iostream_text();
StringBuffer str_text=new StringBuffer();
for (int i = 0; i < 4000; i++) {
str_text = str_text.append("s"); //str_text所能容纳的字符串长度和jvm自身的内存有关
}
File file =new File("src/lab01.txt");
io.printstream(str_text);
io.bufferedwriter(str_text);
io.bufferedreader(file);
io.fileoutputwriter(str_text);
io.fileinputstream(file);
}
}