线程 网络编程

一、线程

1.理解

1)多任务多条路径多个顺序流同时执行就是多线程

2)学习多线程的目标:

  • 1.线程的创建 ***

  • 2.线程的状态 **

  • 3.线程安全 ***

    3)线程优点:提高效率,同时执行

    ​ 缺点:复杂

2.多线程的创建方式

1.继承Thread类,重写run()方法,run方法中定义多线程的线程体

/**
 * 继承Thread重写run(),定义多线程
 * */
public class ThreadDemo01 extends Thread{
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<10;i++){
			System.out.println("kkh");
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建线程
		ThreadDemo01 th=new ThreadDemo01();
		//开启
		th.start();
		for(int i=0;i<10;i++){
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("hhhhhh");
		}

	}
}

2.实现Runnable接口,重写run()方法

  • 推荐:

  • ​ 1.避免单继承的局限性

  • ​ 2.实现资源共享

    public class ThreadDemo02 implements Runnable{
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		//创建线程
    		Thread th=new Thread(new ThreadDemo02());
    		th.start();
    		for(int i=1;i<=100;i++){
    			System.out.println("吃吃");
    			try {
    				//当前线程睡眠指定的时间
    				Thread.sleep(5);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		for(int i=1;i<=100;i++){
    			System.out.println("睡睡");
    			try {
    				//当前线程睡眠指定的时间
    				Thread.sleep(5);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
    

    3.实现Callable接口,重写call方法

3.买票模拟多线程

/*
 * 简单模拟12306例子
 * 	100张票,想要让3个人同时买完这100张票
 *  资源共享:共享资源100张票
 */
public class ThreadDemo03 implements Runnable{
	int tickets=100;
	// A B C
	@Override
	public void run() {
		while(true){
			//停止循环的条件
			synchronized (ThreadDemo03.class) {
				if(tickets<=0){
					break;
				}
				try {
					Thread.sleep(2);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+"正在购买第"+tickets-- +"张票..");
			}
			
		}
	}
	
	public static void main(String[] args) {
		ThreadDemo03 demo=new ThreadDemo03(); //内存空间中只有100张
		//线程的创建
		Thread th1=new Thread(demo,"猪八戒");
		Thread th2=new Thread(demo,"盾山");
		Thread th3=new Thread(demo,"亚瑟");
		//线程的开启
		th1.start();
		th2.start();
		th3.start();
	}

}

//重写方法异常抛出问题:子类重写的方法<=父类方法上异常抛出类型
interface A{
	void test() throws Exception;
}
class B implements A{

	@Override
	public void test() throws NullPointerException{
		// TODO Auto-generated method stub
		
	}
	
}

4.线程状态

线程状态:

  • 新生状态:new创建一个线程的时候

  • 就绪状态:start(),线程会进入就绪队列,等待cpu的调度

  • 运行状态:cpu调用分配时间片给线程,线程就会运行

  • 阻塞状态:sleep()…

  • 终止状态:线程结束了

  • 一个线程一旦就如到阻塞状态,阻塞解除之后,不会马上恢复到运行,会恢复到就绪状态,再次等待cpu的调度

  • 一个线程一旦进入终止状态,再也不会恢复

    如何一个线程进入就绪状态:

  • 1.start()

  • 2.阻塞解除

  • 3.线程切换,被切换的线程会恢复就绪状态

  • 3.yield() 礼让线程

    进入阻塞状态的方式:

  • 1.sleep

  • 2.wait

  • 3.join

    如何控制一个线程终止:

  • 1.stop…不推荐 2.通过表示判断 3.正常执行结束

  • sleep(): 线程睡眠

  • 1)模拟网络延迟

  • 2)放大问题的可能性

  • 让出cpu的资源,不会让出对象的锁(保证资源睡觉),指定休息多少毫秒

  • yield():礼让线程

  • 让出cpu的资源,恢复到就绪状态

  • join():插队线程

5.方法获取线程状态

Thread中 getState() 获取某个线程的状态

  • 线程优先级:
  • 提高先执行的概率
  • getPriority() 返回该线程的优先级。
  • setPriority(int newPriority)改变这个线程的优先级。
  • 1~10 1最小 10最大 默认是5

6.线程安全

线程安全:

​ 多线程同时操作同一份资源才有可能会出现线程不安全的问题,需要控制安全

  • 同步锁: synchronized

  • 同步方法 : 在方法上使用synchronized修饰

    ​ 成员方法,相当于锁this,代码范围为方法

    ​ 静态方法,相当于锁类(类的class对象),代码范围为方法

    为了让线程排队执行{}中的代码

  • 同步块 synchronized(锁的内容){同步的代码范围}

  • 锁的内容: this 类.class 资源(成员变量)

  • 类.class 相当于把这个类,类的所有内容锁住了,类的所有对象都锁住

  • this 当前调用成员方法的对象,相当于把这个对象的所有资源都锁住了,可以只锁资源

  • 资源: 成员变量,一定要是自定义的引用数据类型的对象

  • 锁的范围:{}->中代码的范围

    注意:

  • 锁的范围太大,效率低

  • 锁的范围太小,锁不住

  • 锁不变的内容

    // 人车共用一个街道:
    //  	street 街道   信号灯boolean 
     //  	人 ns -->true
     //  	车 we -->false
    public class StreetDemo03 {
    	public static void main(String[] args) {
    		Street s=new Street();
    		//人车共用街道
    		new Thread(new Person(s)).start();
    		new Thread(new Car(s)).start();
    	}
    }
    
    //街道类  信号灯   true->ns()		false->we()
    class Street{
    	boolean flag= false;  //信号灯
    	
    	//南北走向
    	public synchronized void ns(){
    		if(flag==false){
    			try {
    				this.wait();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}else{
    			try {
    				Thread.sleep(200);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			System.out.println("人走....");
    			flag=false;
    			this.notify();
    		}
    	}
    	
    	//东西走向
    	public synchronized void we(){
    		if(flag==true){
    			try {
    				this.wait();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}else{
    			try {
    				Thread.sleep(200);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			System.out.println("车走....");
    			flag=true;
    			this.notify();
    		}
    	}
    }
    
    class Person implements Runnable{
    	Street street=null;
    	
    	public Person(Street street) {
    		this.street=street;
    	}
    	
    	@Override
    	public void run() {
    		while(true){
    			street.ns();
    		}
    	}
    	 
    

7.两个多线程打印:12A34B56C…

public class StringDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		S s=new S();
		new Thread(new Num(s)).start();
		new Thread(new Cstr(s)).start();
		
	}
}
class S{
	int num=1;
	char c='A';
	boolean flag=false;
	//打印数字二个
	public synchronized void getNum(){
		if(flag==true){
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.print(num+++""+num++);
			
			this.notify();
			flag=true;
		}	
	}
	//打印单个字符
	public synchronized void getChar(){
		if(flag==false){
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.print(c++);
			this.notify();
			flag=false;
		}	
	}
}
class Cstr implements Runnable{
	S s=null;
	public Cstr(S s){
		this.s=s;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			s.getChar();
			if(s.c>'Z'){
				break;
			}
		}
	}	
	
}
class Num implements Runnable{
	S s=null;
	public Num(S s){
		this.s=s;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			s.getNum();
			if(s.num>52){
				break;
			}
		}
	}
}

三、网络编程

1.IP

IP:定位网络上的节点(电脑,手机,服务器,路由器...)  InetAddress
 * 	IP分类:
 * 		ipv4  32* 		ipv6  128* 	特殊的IP:
 * 		非注册ip:供组织内部使用  192.168.0.0~192.168.255.255
 * 		本地ip: 127.0.0.1
 * 	ip->域名:DNS服务器进行转换

2.端口

端口: 区分软件 InetSocketAddress

  • 0~65535之间的整数,2个字节

  • 自定义端口号推荐使用它8000以上

  • 统一协议下端口号不能冲突

  • 8000以内的预留端口号

  • 80 : http

  • 8080 : tomcat

  • 3306 : mysql

  • 1521 : oracle

    url: 统一资源定位符

3.传输层协议

标准,规范,协议

  • udp : 非面向连接 协议简单,开销小,不安全 大小有限制
  • tcp : 基于连接 3次握手 协议复杂,开销大,效率低 安全 大小没有限制
发布了16 篇原创文章 · 获赞 1 · 访问量 470

猜你喜欢

转载自blog.csdn.net/qq_41128049/article/details/104025499
今日推荐