4、getState
作用:获取线程状态
状态种类:
NEW
RUNNABLE
BLOCKED
WAITING
TIMED-WAITING
TERMINATED
5、setPriority
作用:设置优先级
优先级范围:1~10
1最小,10最大;默认值为5
注:优先级不能决定执行顺序,但是可以放大先执行的概率
6、守护线程
线程分类:
用户线程
守护线程
setDeamon():设置守护线程,参数为true则为守护,false为用户线程,默认为用户线程
isDeamon():判断线程是否为守护线程
7、线程安全
多个线程同时操作同一份资源的时候,可能会遇到线程不安全问题
通过锁控制线程数据安全问题
同步锁 synchronized 关键字
关注点:
锁的对象内容->关心锁哪一个对象,能够锁住
同步的代码的范围--> 关心想要哪一个段代码多线程之后需要排序执行
同步的代码范围太大,效率太低,范围太小,可能不安全
锁的对象一定要锁不变的内容,不变的内容才能锁住
synchronized 使用方式:
1.同步方法 : 在方法上synchronized修饰
静态方法
成员方法
2.同步块 使用synchronized修饰{}
synchronized(锁的对象内容){
排队执行的代码段...
} 锁的对象内容 : this | 类.class | 资源
同步方法相对来说比同步块锁同步的代码范围更大,但是比较简单
8、死锁
死锁是指两个或两个以上的线程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,这些永远在互相等待的线程称为死锁线程。
9、Object类中有关线程的方法
1、wait
wait() 等待 : 当一个线程对象调用wait()方法,进入到相关对象的等待池中进行等待,等待阻塞状态,等待被唤醒
会释放cpu的资源,同时释放对象的锁
2、notify
notify() 唤醒 : 唤醒对应对象等待池中正在等待的线程,如果存在多个唤醒某一个
被唤醒的线程会恢复到就绪状态,到底是否能够执行需要cpu的调用,并且获取对象的锁资源
PS:wait()与notify()方法必须存在同步环境内部使用,因为控制多线程之后共享数据存储安全问题
public class PrintChar { public static void main(String[] args) { Char ch=new Char(); Thread a=new Thread(new HighLetter(ch)); Thread b=new Thread(new LowLetter(ch)); a.start(); b.start(); } } class Char{ private boolean flag=true; private char highLetter='A'; private char lowLetter='a'; public char getHighLetter() { return highLetter; } public char getLowLetter() { return lowLetter; } synchronized void letter(){ if(flag){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.print(highLetter++); flag=false; this.notify(); try { if(highLetter>'Z'){ return; } this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } }else { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.print(lowLetter++); flag=true; this.notify(); try { if(lowLetter>'Z'){ return; } this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } class HighLetter implements Runnable{ Char ch; public HighLetter(Char ch) { this.ch = ch; } @Override public void run() { while (ch.getHighLetter()<='Z'){ ch.letter(); } } } class LowLetter implements Runnable{ Char ch; public LowLetter(Char ch) { this.ch = ch; } @Override public void run() { while (ch.getLowLetter()<='z'){ ch.letter(); } } }
二、网络编程
节点:电子设备
IP:定位节点的地址
端口:区分软件
URL:统一资源定位符
传输层协议:
UDP:相当于写信
TCP:相当于打电话
1、IP
IP地址的分类:
IPV4:32位
IPV6:128位
特殊的IP:
1、192.168.0.0~192.168.255.255 私有地址,非注册IP,供组织内部使用 2、127.0.0.1 本地IP(localhost 本地)
IP与域名之间的关系:
IP : 每一个节点的地址
域名 : 对应的IP节点的字符表现形式
DNS解析器 : IP 与 域名之间转换问题
java中的IP类:InetAddress
2、端口
区分软件
2个字节 0~65535
统一协议下端口号不能冲突
8000以内端口号为预留端口号,不建议使用
常见的端口号:
80 http
8080 tomcat
1521 oracle
3306 mysql
java中的端口类:InetSocketAddress
3、URL
互联网的三大基石:
URL
HTTP
HTML
组成:
http://www.baidu.com:80/index.html?username=zhangsan&pssword=123#a
1.协议: http
2.域名: www.baidu.com
3.端口: 80
4.资源: index.html?username=zhangsan&pssword=123#a
URL的方法:
方法名 | 作用 |
---|---|
getProtocol() | 获取协议 |
getHost() | 获取域名 |
getPort() | 获取端口号 |
getFile() | 获取文件 |
getPath() | 获取路径 |
getQuery() | 获取查询部分 |
getRef() | 获取锚点 |
4、网络爬虫
URL:
InputStream openStream() 打开与此 URL的连接并返回 InputStream以从该连接读取。
URL url=new URL("http://www.baidu.com"); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("baidu2.html"),"utf-8")); String msg=null; while((msg=br.readLine())!=null){ bw.append(msg); bw.newLine(); } bw.flush(); bw.close(); br.close();