一:
1.计算机如何存储中文的?
- 当前平台默认编码集 :GBK 一个中文两个字节
- 第一个字节:一定是负数
- 第二个字节:一般是负数,可能也会是正数,不会影响的结果.
-
*/
public class StringDemo {public static void main(String[] args) {
//定义一个字符串
// String str = "abc" ;
// String str = "我爱你中国" ;
String str = "你好" ;//[-60, -29, -70, -61]
//转成字节数组
byte[] bys = str.getBytes() ;
// System.out.println(bys);
//需要能看懂
//Arrays
//System.out.println(Arrays.toString(bys));//[97, 98, 99] :英文的字符串
System.out.println(Arrays.toString(bys));
//[-50, -46, -80, -82, -60, -29, -42, -48, -71, -6]
}
}
2.
一次读取一个字节数组的方式要比一次读取一个字节方式高效.
*
- 一次读取一个字节数组,相当于构造一个缓冲区,有没有比一次读取一个字节数组还要高效的流
- 字节缓冲流 :
-
*/
public class FileInputStreamDemo {public static void main(String[] args) throws IOException {
//复制操作:将e盘下的abc.mp4复制到当前项目下,输出copy.mp4 //封装源文件 FileInputStream fis = new FileInputStream("e://abc.mp4") ; //封装目的地文件 FileOutputStream fos = new FileOutputStream("copy.mp4") ; //读写操作:一次读取字节数组 byte[] bys = new byte[1024] ; int len = 0 ; while((len=fis.read(bys))!=-1) { //边读边写 fos.write(bys, 0, len); } //释放资源 fis.close(); fos.close();
}
}
3.字节缓冲输入流 - public BufferedInputStream(InputStream in):默认缓冲区大小构造缓冲输入流对象
- public BufferedInputStream(InputStream in,int size):指定缓冲区大小构造缓冲输入流对象
- public int read()
- public int read(byte[] b,int off,int len)
-
*在使输入流的时候,
- 两种方式读取(一次读取一个字节/一次读取一个字节数在),只能用一种方式,否则,会出现错误!
- @author Administrator
-
*/
public class BufferedInputStreamDemo {public static void main(String[] args) throws Exception {
//构造一个字节缓冲输入流对象 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt")); //读数据 //一次读取一个字节
/ int by = 0 ;
while((by=bis.read())!=-1) {
System.out.print((char)by);
}///一次读取一个字节数组 byte[] bys = new byte[1024] ; int len = 0 ; while((len=bis.read(bys))!=-1) { System.out.println(new String(bys, 0, len)); } //释放资源 bis.close();
}
}
4.字节缓冲输出流: - 构造方式:
- (第一种开发中) public BufferedOutputStream(OutputStream out):采用的默认的缓冲区大小(足够大了) ,来构造一个字节缓冲输出流对象
- public BufferedOutputStream(OutputStream out,int size):指定size缓冲区大小构造缓冲输出流对象
- IllegalArgumentException - 如果 size <= 0
- 写数据的方式:
- 一次写一个字节
- write(int by)
- 一次写一个字节数组的一部分
- write(byte[] b, int off, int len)
- 方法:
- void flush() ;刷新缓冲区的流
- 面试题:
- 字节缓冲输出流它的构造方法为什么不能直接传递路径/文件?
- 缓冲输入流/缓冲输出流,它只是在底层内部提供一个缓冲区的数组,
- 底层实现文件的复制/读取/写入这些操作都依赖于基本流对象来操作(InputStream/OutputStream/FileInputStream/FileOutputstream)
5.存储文件
- IO流:永久存储(耗时)
- 数据库:永久存储
- 基本的字节流
- 文件字节输入流/文件字节输出流
- 高效的字节流(缓冲流)
- 操作一个视频文件,来测试速度问题
- 基本的字节流一次读取一个字节 ://耗时:85772毫秒
- 基本的字节流一次读取一个字节数组 :共耗时:216毫秒
- 高效的字节流一次读取一个字节 :共耗时:682毫秒
- 高效的字节流一次读取一个字节数组:共耗时:49毫秒
- @author Administrator
-
StringBuffer:提供了一个字符串缓冲区 (可以在缓冲区中不断追加字符串)
/
6.
使用字节流一次读取一个字节的方式,会造成中文乱码--->Java提供了一个字符流(专门用来解决中文乱码问题)
*/
public class FileInputStreamDemo {public static void main(String[] args) throws Exception {
//封装文件 //一次读取一个字节的方式 FileInputStream fis = new FileInputStream("a.txt") ; //读数据 int by = 0 ; while((by=fis.read())!=-1) { System.out.print((char)by); } //释放资源 fis.close();
}
}
7.编码和解码:前后的编码格式要一致! - 编码:
- 简单理解:将能看懂的东西--->看不懂的东西
- 解码:
- 看不懂的东西---能看懂的东西
- 举例: 谍战片
- 今天老地方见...
- 编码:
- 今---->字节---->二进制数据
- 解码:二进制数据-->十进制数据--->字节---->字符串
- 今
- 编码: 将字符串变成一个字节数组
- public byte[] getBytes() :平台默认编码集(默认的是Gbk)
- public byte[] getBytes(Charset charset) ;"指定编码格式
- 解码:将字节数组--->字符串
- public String(byte[] bytes) :使用平台默认编码集(gbk)
- public String(byte[] bytes,Charset charset):用指定的编码格式来解码
- @author Administrator
-
*/
public class StringDemo {public static void main(String[] args) throws Exception {
//定义一个字符串 String str ="你好" ; //编码和解码:前后必须一致 //编码
// byte[] bys = str.getBytes() ;
byte[] bys = str.getBytes("utf-8") ;//-28, -67, -96, -27, -91, -67]
System.out.println(Arrays.toString(bys));//[-60, -29, -70, -61]
System.out.println("------------------");//解码
// public String(byte[] bytes) :使用平台默认编码集(gbk)
// String s = new String(bys) ;
// String s = new String(bys,"gbk") ;//一个中文对应三个字节
String s = new String(bys,"utf-8") ;//一个中文对应三个字节
System.out.println(s);}
}
8.需求:将a.txt文件中的内容进行复制,复制到当前项目下(b.txt) - 文本文件:优先采用字符流
- 源文件:a.txt---->Reader---->InputStreamReader---->FileReader
- 目的的文件:b.txt--->Writer-->OutputStreamWriter---->FileWriter
9.字符转换输入流:InputStreamReader
InputStreamReader(InputStream in) :构造一个字符转换输入流,默认编码
public InputStreamReader(InputStream in,Charset cs) 构造一个字符转换输入流,指定编码
*字符转换输入流=字节流+编码格式
10.字符流: - 字符输入流:Reader
- 字符输出流:Writer
- 字符输出流/字符输入流:都是抽象类
- 使用一个子类:转换流
- 字符输出流的构造方法
- public OutputStreamWriter(OutputStream out):使用默认的编码格式构造一个字符转换输出流对象
- public OutputStreamWriter(OutputStream out, Charset cs):使用指定编码格式构造一个字符转换输出流对象
- 转换流的构成=字节流+编码格式(平台默认/指定)
- 转换流的对象的创建,格式比较长,非常麻烦,Java--->转换流的便捷类
- Reader:抽象类:字符输入流
- inputStreamReader(字符转换输入流 :inputStream+编码格式)
- 便捷类:FileReader,这个类可以直接对文件进行操作
- Writer:抽象类:字符输出流
- outputStreamWriter(字符转换输出流:outputStream+编码格式)
- 便捷类:FileWriter,这个类可以直接对文件进行操作
11.字符输入流读数据的方法:
- int read(char[] chs):读取一个字符数组
- int read():读取单个字符
12.字符输出流写数据的功能: - public void write(int c):写单个字符
- public void write(char[] cbuf):写字符数组
- public abstract void write(char[] cbuf, int off, int len):写字符数组的一部分
- public void write(String str):写字符串
- public void write(String str,int off, int len):写字符串的某一部分
-
*flush和close方法的区别?
- close:关闭该流,关闭该流对象以及它关联 的资源文件,关闭之后,不能再对流对象进行操作了,否则会有异常
- flush:刷新该流,为了防止一些文件(图片文件/音频文件),缺失,或者没有加载到流对象中,刷新了该流,还是可以流对象进行操作
-
*字符缓冲输入流/字符缓冲输出流
-
*杂七杂八的流(properties:属性集合类/合并流/序列化Serializable/内存操作流)
-
*先使用字符缓冲输出流写数据,在使用字符缓冲输入读数据,显示控制台上
-
-
*字符缓冲输出流:
- 特有功能:public void newLine():写入一个行的分隔符号
-
*字符缓冲输入流:
- 特有功能:public String readLine():一次读取一行
14.BufferedReader:字符缓冲输入流 - 构造方法
- public BufferedReader(Reader in)创建一个使用默认大小输入缓冲区的缓冲字符输入流。
- public BufferedReader(Reader in, int sz)创建一个使用指定大小输入缓冲区的缓冲字符输入流。
15.在字符流中提供了一个更高效的流-->字符缓冲流 - 字符缓冲输入流
- 字符缓冲输出流
- BufferedWrier:文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入
- 构造方法
- BufferedWriter(Writer out) :默认缓冲区大小构造字符缓冲输出流对象
- BufferedWriter(Writer out,int size):指定缓冲区大小
16.使用字符缓冲流进行复制操作 -
*分别使用两种方式
- 1)一次读取一个字符数组
- 2)一次读取一行
-
*/
public class CopyDemo {public static void main(String[] args) throws Exception {
//源文件:StringDemo.java
//目的地文件:当前项目下copy.java//封装文件 BufferedReader br = new BufferedReader(new FileReader("StringDemo.java")) ; //封装目的地 BufferedWriter bw = new BufferedWriter(new FileWriter("copy.java")) ; //一次读取一个字符数组
/ char[] chs = new char[1024] ;
int len = 0 ;
while((len=br.read(chs))!=-1) {
bw.write(chs, 0, len);
bw.flush();
}///一次读取一行 String line = null ; while((line=br.readLine())!=null) { //写 bw.write(line); bw.newLine(); bw.flush(); } //关闭资源 bw.close(); br.close();
}
}- 需求:把ArrayList集合中的字符串数据存储到文本文件
- ` ArrayList集合存储的元素String,可以存储一些字符串
- 使用增强for遍历ArrayList
- 使用BufferedWriter(文本文件)
- 源文件:ArrayList<String>
- 目的地:BufferedWriter输出文本文件,给文件中写入字符
-
*/
public class ArrayListToFileTest {public static void main(String[] args) throws Exception {
//创建一个ArrayList集合 ArrayList<String> list = new ArrayList<String>() ; //添加元素 list.add("hello") ; list.add("world") ; list.add("java") ; list.add("hello") ; //创建一个字符缓冲输出流 BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt")) ; //遍历 for(String s:list) { //将集合中元素写入到流中 bw.write(s); bw.newLine(); bw.flush(); } //关闭资源 bw.close();
}
}
18.复制文本文件(5种方式分别完成) - 文本文件:字符流
- 基本的字符流一次读取一个字符
- 一次读取一个字符数组
- 字符缓冲流一次读取一个字符
- 一次读取一个字符数组
- 一次读取一行
- @author Administrator
-
*/
public class CopyTest {public static void main(String[] args) throws Exception {
// method1("StringDemo.java","copy.java") ;
method2();
}
private static void method2() throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader("StringDemo.java")) ;
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.java")) ;
//一次读取一行
String line = null;
while((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
private static void method1(String src, String dest) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(src)) ;
BufferedWriter bw = new BufferedWriter(new FileWriter(dest)) ;
//字符数组
char[] chs = new char[1024] ;
int len = 0 ;
while((len=br.read(chs))!=-1) {
bw.write(chs, 0, len);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
19.需求:有一个文本文本,需要将文本文件中的内容放到ArrayList集合中,遍历集合获取元素
源文件:b.txt----->读取---->BuffferedReader
目的地:ArrayList<String>
*/
public class FileToArrayListTest {
public static void main(String[] args) throws Exception {
//封装源文件
BufferedReader br = new BufferedReader(new FileReader("b.txt")) ;
//创建一个ArrayList集合
ArrayList<String> list = new ArrayList<String>() ;
//读b.txt文件的内容
String line = null ;
while((line=br.readLine())!=null) {
//将数据添加到集合中
list.add(line) ;
}
//遍历集合
for(String s:list) {
System.out.println(s);
}
//关闭流
br.close();
}
}
20.内存操作流:适用于临时存储文件.
- 内存操作输入流:byteArrayInputStream
- ByteArrayInputStream(byte[] buf)
- 内存操作输出流: byteArrayOutputStream
- 构造方法:ByteArrayOutputStream()
*内存操作流:一个程序结束后,那么这些程序的变量,就会从内存消失(马上消失的这些数据进行读取写入) -
21.打印流
- 字符打印流(针对文本进行操作:PrintWriter)
- 字节打印流(printStream 和标准输出流有关系 System.out;)
-
*PrintWriter:属于输出流
- 1)只能写数据(只能针对目的地文件进行操作),不能读数据(不能针对源文件进行操作)
- 2)可以针对文件直接进行操作
- 如果一个类中的构造方法里面有File对象或者String类型数据,这个类可以对文本文件直接操作
- FileInputStream
- FileOutputStream
- FileWriter
- FileReader..
- PrintWriter
- 3)自动刷新功能::PrintWriter(OutputStream out/Writer out,boolean autoflush);第二个参数如果是true 表示启动自动刷新功能
-
4)打印的方法:print(XXX x)/println(XXX xx)
二.
序列化:将对象按照流的方式存储到文本文件中或者再网络中传输 对象---->流数据 序列化流 (ObjectOutputStream)
*反序列化:将文本文件中的流对象或者网络传输中的流对象还原成对象 流数据--->对象 反序列化流(ObjectInputStream) -
现在就自定义类:Person类
/
public class ObjectDemo {public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
// write() ;
read();
}
//反序列化
private static void read() throws FileNotFoundException, IOException, ClassNotFoundException {
//创建反序列化流对象
//public ObjectInputStream(InputStream in)
ObjectInputStream in = new ObjectInputStream(new FileInputStream("oos.txt")) ;
//读
//public final Object readObject():从 ObjectInputStream 读取对象。
Object obj = in.readObject() ;
in.close();
System.out.println(obj);//Person [name=高圆圆, age=27]
}
//序列化
private static void write() throws FileNotFoundException, IOException {
//创建一个序列化流对象
//public ObjectOutputStream(OutputStream out)
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt")) ;
//创建一个Person类对象
Person p = new Person("高圆圆", 27) ;
//public final void writeObject(Object obj)
oos.writeObject(p);
//关闭资源
oos.close();
}
}
2.java.io.NotSerializableException :当前类未实现序列化功能的异常
- Serializable:接口 没有构造方法,没有字段,也没有方法
- 接口---->标记接口
- 自定义类要实现序列化功能,必须实现接口Serializable接口
- 类实现了serializable也意味着他是标记类
- 假设之前操作针对Peroson操作序列的时候,产生一个标记 Preson.class--->固定ID 100
- name -100
- age -100
- 已经序列化完毕了,然后有修改了Person类里面的一些东西,加入了toString()
- Person.class---固定id -- 200
- org.westos_01.Person; local class incompatible:
- stream classdesc serialVersionUID = -428218385429329797,
- local class serialVersionUID = -5865763454468005049
- 因为手动修改了这些类的属性/成员变量,将序列化版本Id改变了
- InvalidClassException:一般情况:该类的序列版本号与从流中读取的类描述符的版本号不匹配
- 实际开发中,不想多次对当前这些序列化,如果这样做,非常麻烦?
- 如何解决呢?
- 让当前实现类序列化功能的这个类产生一个固定ID,注意看程序有×××警告线,直接就点它固定Id
- 比如:当前的这个类中有很多属性(性别,地址,学号...),某些属性不想被序列化,如何解决这样一个问题
3.Properties:表示了一个持久的属性集(简称:属性集合类) extends Hashtable<K,V> Map集合的 - 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
- public Properties():无参构造
-
*/
public class PropertiesDemo {public static void main(String[] args) {
//它继承Hashtable //创建一个属性集合类对象
// Properties<String,String> prop = new Properties<String,String>() ;
Properties prop = new Properties() ;
System.out.println(prop);
System.out.println("---------------------");//给属性集合类中的属性列表添加元素 prop.put("高圆圆", "赵又廷") ; prop.put("文章", "马伊琍") ; prop.put("黄晓明", "baby") ; System.out.println(prop); //遍历属性集合类 Set<Object> keySet = prop.keySet() ; for(Object key :keySet) { Object value = prop.get(key) ; System.out.println(key+"="+value); }
}
}
4.Properties:表示了一个持久的属性集(简称:属性集合类) extends Hashtable<K,V> Map集合的 - 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
- public Properties():无参构造
-
*/
public class PropertiesDemo {public static void main(String[] args) {
//它继承Hashtable //创建一个属性集合类对象
// Properties<String,String> prop = new Properties<String,String>() ;
Properties prop = new Properties() ;
System.out.println(prop);
System.out.println("---------------------");//给属性集合类中的属性列表添加元素 prop.put("高圆圆", "赵又廷") ; prop.put("文章", "马伊琍") ; prop.put("黄晓明", "baby") ; System.out.println(prop); //遍历属性集合类 Set<Object> keySet = prop.keySet() ; for(Object key :keySet) { Object value = prop.get(key) ; System.out.println(key+"="+value); }
}
}
5.属性集合类的特有功能: - public Object setProperty(String key, String value) :给属性列表中添加键和值,并且强制都使用String
- public Set<String> stringPropertyNames():遍历的功能
- public String getProperty(String key)用指定的键在此属性列表中搜索属性
-
*/
public class PropertiesDemo2 {public static void main(String[] args) {
//创建属性集合类对象 Properties prop = new Properties() ; //添加元素 prop.setProperty("张三", "20") ; prop.setProperty("李四", "22") ; prop.setProperty("王五", "18") ; //遍历 //获取所有的键的集合 Set<String> keyset = prop.stringPropertyNames() ; for(String key:keyset) { //通过键找值 String value = prop.getProperty(key) ; System.out.println(key+"----"+value); }
}
}
6.可保存在流中或从流中加载,只能使用属性集合类
public void store(Writer writer,String comments):把集合中的数据保存文本文件中(属性集合)
public void load(Reader reader):将文本文件中的数据加载到属性集合中
7.可保存在流中或从流中加载,只能使用属性集合类
public void store(Writer writer,String comments):把集合中的数据保存文本文件中(属性集合)
public void load(Reader reader):将文本文件中的数据加载到属性集合中 -
*举例:
-
打游戏:游戏进度的保存和游戏加载
-
*/
public class PropertiesDemo3 {public static void main(String[] args) throws IOException {
// MyStore();
MyLoad();
}
//将文本文件中的数据加载属性集合类中
private static void MyLoad() throws IOException {
//创建属性集合类对象
Properties prop =new Properties() ;
//public void load(Reader reader):将文本文件中的数据加载到属性集合中
FileReader fr = new FileReader("prop.txt") ;
//加载
prop.load(fr);
fr.close();
System.out.println(prop);
}
//将属性集合中的数据保存到文本文件中
private static void MyStore() throws IOException {
//创建一个属性集合类对象
Properties prop = new Properties() ;
//添加元素
prop.setProperty("张三", "20") ;
prop.setProperty("文章", "29") ;
prop.setProperty("成龙", "55") ;
//public void store(Writer writer,String comments):把集合中的数据保存文本文件中(属性集合)
FileWriter fw = new FileWriter("name.txt") ;
//将数据保存到文本文件中
prop.store(fw, "names'content");
//释放资源
fw.close();
}
}
-
我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。
请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”1001)读取文件的内容,将文件内容加载属性集合类中
2)遍历属性集合,获取所有的键的集合
3)遍历的键的时候,可以判断是否有"lisi"这样一个键
4)有的话,就更改
5)需要将当前属性集合类中的保存文本文件中
/
public class PropertiesTest {public static void main(String[] args) throws IOException {
//创建属性集合类对象 Properties prop = new Properties() ; //读取文本文件内容加载到集合中 FileReader fr = new FileReader("user.txt") ; prop.load(fr); fr.close(); //遍历属性集合 //获取所有的键的集合 Set<String> keySet = prop.stringPropertyNames() ; for(String key:keySet) { //判断 if("lisi".equals(key)) { //更改 prop.setProperty(key, "100") ; } } //将属性集合中的数据保存文本文件中 FileWriter fw = new FileWriter("user.txt") ; prop.store(fw, "content"); fw.close();
}
}
9.JVM:Java虚拟机 识别main(主线程)- 面试题:
- JVM是多线程程序吗?至少有几条线程..
- jvm是多线程的,
- 至少有2条线程...
- 有主线程,main..执行这些代码,能够被Jvm识别
- 在执行一些程序的时候,一些对象Jvm释放掉,原因,
- 它开启了垃圾回收线程,里面GC:垃圾回收器(回收一些没有更多引用的对象或者变量...)
10.如何实现多线程程序呢?
要实现多线程程序,需要开启进程,
开启进程,是需要创建系统资源,但是Java语言不能创建系统资源
只有C/C++可以创建系统资源, 利用c语言创建好的系统资源实现
Java提供了一个类:Thread类 - 实现多线程程序的步骤:
- 1)将类声明为 Thread 的子类
- 2)该子类应重写 Thread 类的 run 方法
- 3)在主线程进行该自定义的线程类的对象的创建
-
并行和并发(高并发:MyBatis --->IBatis:半自动化)
强者逻辑上的同时,指的是同一个时间段内
*后者物理上的同时,指的是同一个时间点 -
*/
public class ThreadDemo {
public static void main(String[] args) {
//创建MyThread类对象
// MyThread my = new MyThread() ;
//当前Thread类有一个run public void run()
// my.run();
// System.out.println("-------------------");
// my.run();
//执行线程不是run方法 ,run方法的调用相当于一个普通方法的调用/*public void start()使该线程开始执行;Java 虚拟机调用该线程的 run 方法。 结果是两个线程并发地运行*/ MyThread t1 = new MyThread() ; t1.start(); //IllegalThreadStateException:非法状态异常,同一个线程只能被执行一次
// t1.start();
MyThread t2 = new MyThread() ;
t2.start();}
}
11.Thread 类提供了一些方法- public final void setName(String name):给线程起名称
- public final String getName() :获取线程名称
-
12.*public final void setDaemon(boolean on) :true时,表示为守护线程
- 将该线程标记为守护线程或用户线程。当正在运行的线程都是守护线程时,Java 虚拟机退出。(守护线程不会立即结束掉,它会执行一段时间在结束掉)
- 该方法必须在启动线程前调用。
-
13.跟线程优先级相关的方法:
- public final int getPriority()返回线程的优先级。
- public final void setPriority(int newPriority)更改线程的优先级
- 线程存在一个默认优先级
-
public static final int MAX_PRIORITY 10 最大优先级
public static final int MIN_PRIORITY 1 最小优先级
public static final int NORM_PRIORITY 5 默认优先级 -
*/
public class ThreadDemo {
public static void main(String[] args) {
//创建三个子线程 MyThread t1 = new MyThread() ; MyThread t2 = new MyThread() ; MyThread t3 = new MyThread() ;
// System.out.println(t1.getPriority()); //5 默认优先级
// System.out.println(t2.getPriority());
// System.out.println(t3.getPriority());
t1.setName("林青霞");
t2.setName("林志颖");
t3.setName("×××");
//设置线程优先级
t1.setPriority(10);
t2.setPriority(1);
t3.setPriority(5);
t1.start();
t2.start();
t3.start();
}
}
14.public static void sleep(long millis):线程睡眠 指定是时间毫秒值
- throws InterruptedException
- 两个区别?
public final void stop() ;强迫线程停止执行。 不会执行了 (过时了),方法能使用的
public void interrupt()中断线程。 表示中断线程的一种状态
面试题
区别?
wait(): wait()调用的,立即释放锁 (同步锁/Lock锁)
sleep(): 线程睡眠,调用不会释放锁- public static void yield()暂停当前正在执行的线程对象,并执行其他线程
- 实现多线程程序的第二种方式:
- 1)自定义一个类,实现Runnable接口
- 2)实现接口中的run方法,对耗时的代码进行操作
- 3)然后在主线程中创建该了对象,将该类对象做为一个资源类,创建Threadd类的对象,将刚才的资源类作为参数进行传递
-
*/
public class ThreadDemo {public static void main(String[] args) {
//创建当前类对象 MyThread my =new MyThread() ; //实现多线程 //public Thread(Runnable target,String name) Thread t1 = new Thread(my, "高圆圆") ; Thread t2 = new Thread(my, "赵又廷") ; //启动线程 t1.start(); t2.start();
}
}