多线程 一

多线程概述
多线程实现方案
线程调度和线程控制
线程生命周期
线程同步
死锁
线程间通信
定时器的使用

多线程概述
多线程概述
进程:
正在运行的程序,是系统进行资源分配和调用的独立单位。
每一个进程都有它自己的内存空间和系统资源。

线程:
是进程中的单个顺序控制流,是一条执行路径
一个进程如果只有一条执行路径,则称为单线程程序。
一个进程如果有多条执行路径,则称为多线程程序。

Java程序运行原理
java 命令会启动 java 虚拟机,启动 JVM,等于启动了一个应用程序,也就是启动了一个进程。该进程会自动启动一个 “主线程” ,然后主线程去调用某个类的 main 方法。所以 main方法运行在主线程中。在此之前的所有程序都是单线程的。

实现多线程的程序。

方法一
方式1:继承Thread类。

  • 步骤
  •  A:自定义类MyThread继承Thread类。
    
  •  B:MyThread类里面重写run()?
    
  • 将要多线程执行的代码写到run方法内部
  •  C:创建对象
    
  •  D:启动线程       start();首先启动了线程,然后再由jvm去调用该线程的run()方法。
    
  • 直接调用run是单线程
MyThread my1 = new MyThread();
		MyThread my2 = new MyThread();

		my1.start();
		my2.start();

Thread类的基本获取和设置方法
public final String getName():获取线程的名称。
public final void setName(String name):设置线程的名称
其实通过构造方法也可以给线程起名字
public static Thread currentThread():返回当前正在执行的线程对象
Thread.currentThread().getName() 获取任意方法所在的线程名称


		// 创建线程对象
		//无参构造+setXxx()
		// MyThread my1 = new MyThread();
		// MyThread my2 = new MyThread();
		// //调用方法设置名称
		// my1.setName("林青霞");
		// my2.setName("刘意");
		// my1.start();
		// my2.start();
		
		//带参构造方法给线程起名字
		// MyThread my1 = new MyThread("林青霞");
		// MyThread my2 = new MyThread("刘意");
		// my1.start();
		// my2.start();
		
		//我要获取main方法所在的线程对象的名称,该怎么办呢?
		//遇到这种情况,Thread类提供了一个很好玩的方法:
		//public static Thread currentThread():返回当前正在执行的线程对象
		System.out.println(Thread.currentThread().getName());
	

线程调度
分时调度模型 所有线程轮流使用 CPU 的使用权,平均分配每个线程占用 CPU 的时间片
抢占式调度模型 优先让优先级高的线程使用 CPU,如果线程的优先级相同,那么会随机选择一个,优先级高的线程获取的 CPU 时间片相对多一些。
Java使用的是抢占式调度模型。
public final int getPriority(): 返回线程对象的优先级
public final void setPriority(int newPriority): 更改线程的优先级。

默认优先值为5 线程优先级的范围是:1-10。


		ThreadPriority tp1 = new ThreadPriority();
		ThreadPriority tp2 = new ThreadPriority();
		ThreadPriority tp3 = new ThreadPriority();

		tp1.setName("东方不败");
		tp2.setName("岳不群");
		tp3.setName("林平之");

		// 获取默认优先级
		// System.out.println(tp1.getPriority());
		// System.out.println(tp2.getPriority());
		// System.out.println(tp3.getPriority());

		// 设置线程优先级
		// tp1.setPriority(100000);//会出现IllegalArgumentException异常
		//设置正确的线程优先级
		tp1.setPriority(10);
		tp2.setPriority(1);

		tp1.start();
		tp2.start();
		tp3.start();
	

线程控制
线程休眠
public static void sleep(long millis)

public class ThreadSleep extends Thread {
	@Override
	public void run() {
		for (int x = 0; x < 100; x++) {
			System.out.println(getName() + ":" + x + ",日期:" + new Date());
			// 睡1秒钟
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

线程加入
public final void join() 等待该线程终止。


	public static void main(String[] args) {
		ThreadJoin tj1 = new ThreadJoin();
		ThreadJoin tj2 = new ThreadJoin();
		ThreadJoin tj3 = new ThreadJoin();

		tj1.setName("李渊");
		tj2.setName("李世民");
		tj3.setName("李元霸");

		tj1.start();
		try {
			tj1.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		tj2.start();
		tj3.start();
	}

线程礼让
public static void yield() 暂停当前正在执行的线程对象,并执行其他线程。
让多个线程的执行更和谐,但是不能靠它保证一人一次。

public class ThreadYield extends Thread {
	@Override
	public void run() {
		for (int x = 0; x < 100; x++) {
			System.out.println(getName() + ":" + x);
			Thread.yield();
		}
	}

后台线程
public final void setDaemon(boolean on) 将该线程标记为守护线程或用户线程。
该方法必须在启动线程前调用。
当正在运行的线程都是守护线程时,Java 虚拟机退出。


		ThreadDaemon td1 = new ThreadDaemon();
		ThreadDaemon td2 = new ThreadDaemon();

		td1.setName("关羽");
		td2.setName("张飞");

		// 设置守护线程
		td1.setDaemon(true);
		td2.setDaemon(true);

		td1.start();
		td2.start();

		Thread.currentThread().setName("刘备");
		for (int x = 0; x < 5; x++) {
			System.out.println(Thread.currentThread().getName() + ":" + x);
		}
	

中断线程
public final void stop() 让线程停止,
public void interrupt() 中断线程 抛出一个InterruptedException。



public class ThreadStopDemo {
	public static void main(String[] args) {
		ThreadStop ts = new ThreadStop();
		ts.start();

		// 你超过三秒不醒过来,我就干死你
		try {
			Thread.sleep(3000);
			// ts.stop();//强制结束 不合适
			ts.interrupt();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

public class ThreadStop extends Thread {
	@Override
	public void run() {
		System.out.println("开始执行:" + new Date());

		// 我要休息10秒钟,亲,不要打扰我哦
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// e.printStackTrace();
			System.out.println("线程被终止了");
		}

		System.out.println("结束执行:" + new Date());
	}
}

线程的生命周期图
在这里插入图片描述

多线程的实现方案2

实现Runnable接口

步骤:

  •  A:自定义类MyRunnable实现Runnable接口
    
  •  B:重写run()方法
    
  •  C:创建MyRunnable类的对象
    
  •  D:创建Thread类的对象,并把C步骤的对象作为构造参数传递
    
public class MyRunnable implements Runnable {

	@Override
	public void run() {
		for (int x = 0; x < 100; x++) {
			// 由于实现接口的方式就不能直接使用Thread类的方法了,但是可以间接的使用
			System.out.println(Thread.currentThread().getName() + ":" + x);
		}
	}

}

public class MyRunnableDemo {
	public static void main(String[] args) {
		// 创建MyRunnable类的对象
		MyRunnable my = new MyRunnable();

		// 创建Thread类的对象,并把C步骤的对象作为构造参数传递
		// Thread(Runnable target)
		// Thread t1 = new Thread(my);
		// Thread t2 = new Thread(my);
		// t1.setName("林青霞");
		// t2.setName("刘意");

		// Thread(Runnable target, String name)
		Thread t1 = new Thread(my, "林青霞");
		Thread t2 = new Thread(my, "刘意");

		t1.start();
		t2.start();
	}
}
	

练习(售卖电影票)和同步锁


两种方式实现
继承Thread类

public class SellTicket extends Thread {

	// 定义100张票
	// private int tickets = 100;
	// 为了让多个线程对象共享这100张票,我们其实应该用静态修饰
	private static int tickets = 100;

	@Override
	public void run() {
		// 定义100张票
		// 每个线程进来都会走这里,这样的话,每个线程对象相当于买的是自己的那100张票,这不合理,所以应该定义到外面
		// int tickets = 100;

		// 是为了模拟一直有票
		while (true) {
			if (tickets > 0) {
			try {
					Thread.sleep(100); //t1进来了并休息,t2进来了并休息,t3进来了并休息,
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(getName() + "正在出售第" + (tickets--) + "张票");
			}
		}
	}
}

public class SellTicketDemo {
	public static void main(String[] args) {
		// 创建三个线程对象
		SellTicket st1 = new SellTicket();
		SellTicket st2 = new SellTicket();
		SellTicket st3 = new SellTicket();

		// 给线程对象起名字
		st1.setName("窗口1");
		st2.setName("窗口2");
		st3.setName("窗口3");

		// 启动线程
		st1.start();
		st2.start();
		st3.start();
	}
}

实现Runnable接口

public class SellTicket implements Runnable {
	// 定义100张票
	private int tickets = 100;

	@Override
	public void run() {
		while (true) {
			if (tickets > 0) {
			try {
					Thread.sleep(100); //t1进来了并休息,t2进来了并休息,t3进来了并休息,
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName() + "正在出售第"
						+ (tickets--) + "张票");
			}
		}
	}
}

public class SellTicketDemo {
	public static void main(String[] args) {
		// 创建资源对象
		SellTicket st = new SellTicket();

		// 创建三个线程对象
		Thread t1 = new Thread(st, "窗口1");
		Thread t2 = new Thread(st, "窗口2");
		Thread t3 = new Thread(st, "窗口3");

		// 启动线程
		t1.start();
		t2.start();
		t3.start();
	}
}

加入sleep 会出现卖重复票和负数票的问题


解决线程安全问题实现1


同步代码块:

  •  synchronized(对象){
    
  •  	需要同步的代码;
    
  •  }
    

同步可以解决安全问题的根本原因就在那个对象上。该对象如同锁的功能。

同步方法的格式及锁对象问题?

  •  把同步关键字加在方法上。
    
  •  同步方法是谁呢?
    
  •  	this
    

同步的前提
多个线程
多个线程使用的是同一个锁对象

同步的好处
同步的出现解决了多线程的安全问题。

同步的弊端
当线程相当多时,因为每个线程都会去判断同步上的锁,这是很耗费资源的,无形中会降低程序的运行效率。

同步代码块:

public class SellTicket implements Runnable {
	// 定义100张票
	private int tickets = 100;
	//创建锁对象
	private Object obj = new Object();

	@Override
	public void run() {
		while (true) {
			synchronized (obj) {
				if (tickets > 0) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName()
							+ "正在出售第" + (tickets--) + "张票");
				}
			}
		}
	}
}


public class SellTicketDemo {
	public static void main(String[] args) {
		// 创建资源对象
		SellTicket st = new SellTicket();

		// 创建三个线程对象
		Thread t1 = new Thread(st, "窗口1");
		Thread t2 = new Thread(st, "窗口2");
		Thread t3 = new Thread(st, "窗口3");

		// 启动线程
		t1.start();
		t2.start();
		t3.start();
	}
}

同步方法 锁对象为this 静态方法锁为class文件

public class SellTicket implements Runnable {

	// 定义100张票
	private static int tickets = 100;

	// 定义同一把锁
	private Object obj = new Object();
	
	private int x = 0;
	

	@Override
	public void run() {
		while (true) {
			if(x%2==0){
				synchronized (SellTicket.class) {//当锁对象为方法 则是this
					if (tickets > 0) {
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						System.out.println(Thread.currentThread().getName()
								+ "正在出售第" + (tickets--) + "张票 ");
					}
				}
			}else {

				
				sellTicket();
				
			}
			x++;
		}
	}


	
	private static synchronized void sellTicket() {
		if (tickets > 0) {
		try {
				Thread.sleep(100);
		} catch (InterruptedException e) {
				e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()
					+ "正在出售第" + (tickets--) + "张票 ");
		}
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值