总结: TCP协议特点:面向有连接(先建立连接,后建立数据) UDP协议特点:面向无连接(只需要发送数据,不需关心对方是否存在) TCP协议两个常用名称 Socket:客户端类 •构造方法 public Socket(String ip,int port);//服务器IP地址,服务器端口号 •常用方法 public OutputStream getOutputStream();//获取连接通道中的输出流 public InputStream getInputStream();//获取连接通道中的输入流 public void shutDownOutput();//关闭连接通道中的输出流 public void shutDownInput();//关闭连接通道中的输入流 public void close();//关闭客户端对象
ServerSocket:服务器类 •构造方法 public ServerSocket(int port);//指定服务器端使用的端口号 •常用的成员方法 public Socket accept();//接收连接到服务器的Socket对象,如果暂时没有客户端,该方法会阻塞 public Socket close();//关闭服务器对象
4.2 PrintStream的构造和常用方法 构造方法: public PrintStream(String Path);//直接指定路径 public PrintStream(File file);//直接指定文件 public PrintStream(OutputStream out);//先给一个输出流,绑定什么对象就打印到什么对象
成员方法: public void print(各种类型);//不带换行的打印 public void println(各种类型);//带换行的打印
字符输入流 顶层父类:Reader(抽象类) 共性方法: public void close();//释放资源 public int read();//一次读一个char字符,返回字符ASCII码值,为int类型 public int read(char[] chs);//一次读一个char字符数组,返回值表示实际读取的字符个数
FileReader类的使用 文件的字符输入流(从文件中读取字符数据的)
•构造方法 public FileReader(String Path); public FileReader(File file);
字符输出流 顶层父类:Writer(抽象类) 共性方法: public void close();//释放资源 public int flush();//对于字符串游泳 public int write();//一次写一个char字符,返回字符ASCII码值,为int类型 public int write(char[] chs);//一次写一个char字符数组 public int write(char[] chs,int startIndex,int len);//一次写一个char字符数组的一部分
public write(String str);//直接写一个字符串 public write(String str,int startIndex,int len);//直接写一个字符串的一部分
FileWriter类的使用 文件的字符输出流(向文件中写字符数据) •构造方法 public FileWriter(String Path); public FileWriter(File file);
总结: Java四大流: -字节输出流OutputStream: 子类:FileOutputStream public void close();//关闭该流,释放资源 public void flush();//刷新缓冲区(主要字符流使用) public void write(int b);//一次写一个字节,输入是int,但是只能写一个byte的大小,即最大127 public void write(byte[] bs);//一次写一个字节数组 public void write(byte[] bs,int startIndex,int len);//一次写这一个字节数组中的一部分
构造方法三件事: 创建输出流对象 若存在覆盖,若不存在创建 释放资源
-字节输入流InputStream: 子类:FileInputStream public void close();//关闭该流,释放资源 public int read();//一次读一个字节 public int read(byte[] bs);//一次读一个字节数组,返回值表示实际读取的字节个数 public int read(byte[] bs,int startIndex,int len);//一次读一个字节数组的一部分(基本不用)
构造方法三件事: 创建输入流对象 若存在则读取,若不存在报错 释放资源
-字符输出流Writer: 子类:FileWriter public void close();//释放资源 public void flush();//刷新缓冲区
public int write();//一次写一个char字符,返回字符ASCII码值,为int类型 public int write(char[] chs);//一次写一个char字符数组 public int write(char[] chs,int startIndex,int len);//一次写一个char字符数组的一部分 public write(String str);//直接写一个字符串 public write(String str,int startIndex,int len);//直接写一个字符串的一部分
构造方法三件事: 创建字符输出流对象 若存在覆盖,若不存在创建 释放资源
-字符输入流Reader: public void close();//释放资源 public int read();//一次读一个char字符,返回字符ASCII码值,为int类型 public int read(char[] chs);//一次读一个char字符数组,返回值表示实际读取的字符个数
排序的工具类 对数组进行排序:Arrays.sort(array,new Comparator<数组元素类型>);//必须引用类型 对List集合排序:Collections.sort(,new Comparator<集合中元素对类型>); 对Set集合排序: 并不是所有的Set都能排序,TreeSet才能排序:TreeSet set = new TreeSet(new 比较器对象()); TreeSet排序:TreeSet set = new TreeSet(new 比较器对象());//或自己写排序算法:冒泡,选择,插入,希尔,快速,堆,归并…
1.3 File类的获取方法 public String getAbsolutePath();//获取该File对象的绝对路径 public String getPath();//获取该File对象构造时,传入的对象 public String getName();//获取该File对象的代表的文件或文件夹的名字 public long length();//获取该File对象的文件字节
4.2 字节输出流 顶层父类(抽象类) 共性方法: public void close();//关闭该流,释放资源 public void flush();//刷新缓冲区(主要字符流使用)
public void write(int b);//一次写一个字节,输入是int,但是只能写一个byte的大小,即最大127 public void write(byte[] bs);//一次写一个字节数组 public void write(byte[] bs,int startIndex,int len);//一次写这一个字节数组中的一部分
4.4 FileOutputStream类的使用 文件的字节输出流(向文件中写字节数据)
•构造方法 public FileOutputStream(String path);//必须传入文件的路径 public FileOutputStream(File file);//必须传入文件的File对象
publicclassFileOutputStream_Demo04 { publicstaticvoidmain(String[] args)throws IOException { FileOutputStreamfos=newFileOutputStream("1.txt",true); for (inti=0; i < 10; i++) { fos.write("java \n".getBytes()); } } }
•flush public void flush();//对于字节输出流没用
•close public void close();//关闭该流,释放资源 一个流使用完毕,及时释放流,别的程序就可以使用该资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicclassFileOutputStream_Demo04 { publicstaticvoidmain(String[] args)throws IOException { FileOutputStreamfos=newFileOutputStream("1.txt", true); for (inti=0; i < 10; i++) { fos.write("java \n".getBytes()); }
fos.close();
while (true) { } //程序不会停止,但是流已关闭,可以对文件进行增删改操作 } }
4.4 字节输入流InputStream 顶层父类(抽象类) 共性方法: public void close();//关闭该流,释放资源 public int read();//一次读一个字节 public int read(byte[] bs);//一次读一个字节数组,返回值表示实际读取的字节个数 public int read(byte[] bs,int startIndex,int len);//一次读一个字节数组的一部分(基本不用)
4.5 FileInputStream的作用 文件的字节输入流(向文件中读字节数据) •构造方法 public FileInputputStream(String path);//必须传入文件的路径 public FileInputStream(File file);//必须传入文件的File对象
工具类中的静态方法:创建一个线程池对象 public static ExecutorService newFixedThreadPool(int nThreads);//用于创建一个具有指定线程个数的线程池对象
如何向线程池中提交任务: 调用ExecutorService接口中规定的方法 public Future submit(Runnable task);//向线程池中提交无返回值的任务 public Future submit(Callable task);//向线程池中提交有返回值的任务,返回Future类型(封装线程执行完毕后结果的那个对象)
三.线程状态 3.1 线程的六种状态(A thread state. A thread can be in one of the following states:) •新建状态-NEW A thread that has not yet started is in this state. 刚创建,且未调用strat方法的状态
•运行状态-RUNNABLE A thread executing in the Java virtual machine is in this state. 处于新建状态的线程start方法之后 可运行状态(就绪) 可运行状态(运行) 注意:只有新建状态的线程才能调用start()
•受(锁)阻塞状态-BLOCKED A thread that is blocked waiting for a monitor lock is in this state. 状态进入: 线程运行过程中,遇到同步方法,线程需要锁对象,但是锁对象已被其他线程持有 状态返回: 其他线程释放锁,当前线程抢到锁,才能从锁阻塞回到可运行状态
•限时等待状态-TIMED_WAITING A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. 状态进入: 线程指定到代码调用Thread.slee(毫秒值),线程就处于限时等待状态 状态返回: sleep时间到了,返回RUNNABLE状态
•永久等待-WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state. 状态进入: 当前线程必须持有锁对象 调用锁对象的wait()方法 此时线程就会进入永久等待,进入之前,当前线程会自动释放锁对象 状态返回: 其他线程要持有锁(必须锁刚刚进入无限等待的线程释放的锁) 调用锁对象的notify()方法
注意:被唤醒的线程是没有锁对象的,会进入锁阻塞,直到其他线程释放锁对象,才能进入可运行状态
•TERMINATED A thread that has exited is in this state. 当线程任务执行完毕,已退出的线程状态(消亡状态) 注意:处于消亡状态的线程,不能再调用start方法
//create thread newThread(newRunnable() { @Override publicvoidrun() { synchronized (lock) { //get in code block System.out.println("Thread A get into Wating"); System.out.println("Thread A Get Lock"); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } //wake back to task System.out.println("A Wake"); } } }).start();
Thread.sleep(1000);
//New Threak wake up Thread A newThread(newRunnable() { @Override publicvoidrun() { //Same lock Object synchronized (lock) { System.out.println("Thread B Got Lock"); System.out.println("Thread B Wake A Up"); lock.notify(); for (inti=0; i < 10; i++) { System.out.println(i); } } } }).start(); } }
1.6 解决方法3:Lock锁 Lock是一个接口,我们需要使用其实现类ReentrantLock ReentrantLock的API: public ReentrantLock(); public void lock();//加锁 public void unlock();//解锁 格式: ReentrantLock lock = new ReentrantLock(); lock.lock();//加锁 lock.unloc();//解锁
publicclassMythreadextendsThread{ // public static List<Integer> list = new ArrayList<>();//ArrayIndexOutOfBoundsException publicstatic List<Integer> list = newCopyOnWriteArrayList<>();
@Override publicvoidrun() { for (inti=0; i < 10000; i++) { list.add(i); } System.out.println("Done"); } }
publicclassMythreadextendsThread { // public static List<Integer> list = new ArrayList<>(); publicstatic List<Integer> list = newCopyOnWriteArrayList<>(); @Override publicvoidrun() { for (inti=0; i < 10000; i++) { list.add(i); } System.out.println("Done"); } }
b.Thread类的构造方法 public Thread();//无参构造 public Thread(String name);//带有线程名的构造 public Thread(Runnable r);//带有线程任务的构造 public Thread(Runnable r,String name);//即带有线程名字,又有线程任务的构造
c.Thread类的成员方法 public String getName();//获取线程的名字。无参构造,线程会有自己的名字,如Thread0,Thread1 public String setName(String name);//修改线程的名字 public void run();//代表线程要执行的任务,任务有关的代码需要写在此方法中 public void start();//线程只创建并不会执行,必须调用start开启后才会执行任务
public static void sleep(long millis);//让当前线程休眠X毫秒(指Thread.sleep(1000)代码写在哪个线程中,所在的线程就当前线程) public static Thread currentThread();//获取当前线程对象(指Thread.currentThread()代码写在哪个线程中,所在的线程就当前线程)
public static void join();//等待这个线程死亡 public static void setDaemon(boolean on);//将此线程标记为守护线程,当允许的线程都是守护线程时,Java虚拟机将退出
public final int getPriority();//返回此线程的优先级 public final void setPriority(int newPriority);//更改此线程的优先级 线程优先级高,仅表示获取CPU时间片的几率高,并不表示会优先执行
1.4 创建新的线程方式一:继承方式 a. API描述:将类声明为Thread的子类。该子类重写Thread类的run方法。接下来可以分配并启动该子类的实例 b.分析创建步骤: I.创建子类 继承 Thread II.子类中重写run方法(在run中编写线程要执行的任务代码)
4.2 AtomicInteger类示例 a.AtomicInteger是什么:是对int类型变量进程操作原子类 b.AtomicInteger的构造方法:public AtomicInteger(int num); c.AtomicInteger的成员方法: public int getAndIncrement();//就相当于我们的 变量++ public int ncrementAndGet();//就相当于我们的 ++变量
方法名 说明 public void printStackTrace(); 以深红色在控制台打印异常的详细信息(包括异常类型,异常的原因,异常的位置)【最常用】 public String getMessage(); 返回此throwable的详细消息字符串 public String toString(); 获取异常的类型和异常的描述信息 打印方法如下: