0.相关概念
并行:多个事件同一时间段内发生。
并发:多个事件同一时间发生。
进程:是程序运行的基本单位,一个运行的程序可以同时运行多个进程。
线程:是进程的一个执行单元,一个进程可以有多个线程。
1.线程创建的三种方式
1.1继承Thread类
package thread;
public class Mythread extends Thread {
@Override
public void run() {
System.out.println("继承Thread");
}
public static void main(String[] args) {
Mythread mythread = new Mythread();
mythread.start();
}
}
1.2实现runnable接口(最常用,最重要)
1.2.1方法一:通常方法
package thread;
public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("实现Runnable");
}
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable();
Thread t = new Thread(runnable);
t.start();
}
}
1.2.2方法二:匿名内部类
package thread;
public class MyRunnable {
public static void main(String[] args) {
//1
/*Runnable runnable = new Runnable(){
@Override
public void run() {
System.out.println("匿名内部类实现Runnable");
}
};
new Thread(runnable).start();//静态代理*/
//2
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("匿名内部类简化");
}
}).start();
}
}
}
}
1.2.3方法三:Lambda表达式
package thread;
public class MyRunnableLambda {
public static void main(String[] args) {
new Thread(()-> System.out.println("Lambda表达式实现Runnable接口")).start();
}
}
1.2.3
1.3实现Callable接口(不常用,不建议)
多了返回值,可以抛出异常
class MyCallable implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("Callable这个线程被创建了!!!");
return;
}
}
2.线程方法
2.1线程停止
2.1.1建议线程正常停止–>利用次数,不建议死循环
2.1.2建议使用标志位–>设置一个标志位
2.1.3不要使用stop或者destroy等过时或者jdk官网不建议使用的方法
public class Teststop implements Runnable {
private boolean flag = true;
@Override
public void run() {
int i = 0;
System.out.println("线程在运行" + (i++));
}
public void stop() {
this.flag = false;
}
public static void main(String[] args) {
Teststop teststop = new Teststop();
new Thread(teststop).run();
for (int i = 0; i < 1000; i++) {
System.out.println("main" + i);
if (i == 900) {
teststop.stop();
System.out.println("线程该停止了");
}
}
}
}
2.2线程休眠
在程序中允许一个线程进行暂时的休眠,直接使用 Thread.sleep() 即可实现休眠。
public class Testsleep {
public static void main(String[] args) throws InterruptedException {
Date starttime = new Date(System.currentTimeMillis());
while (true){
Thread.sleep(1000);//休眠一秒
System.out.println(new SimpleDateFormat("HH:mm:ss").format(starttime));
starttime = new Date(System.currentTimeMillis());//更新当前时间
}
}
}
2.3线程礼让
Thread.yield(); 让当前线程让出时间片,再竞争一次,CPU重新调度,礼让不一定成功。
// 礼让线程,让出当前执行线程,但不阻塞,运行状态改成就绪状态
// 礼让不一定成功,要看CPU的心情
public class TestYield {
public static void main(String[] args) {
Myyield myyield = new Myyield();
new Thread(myyield,"a").start();
}
}
class Myyield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程结束");
}
}
2.4线程强制执行
thread.join(); 在一个线程里面强行插入另外一个线程,直到插入的线程执行结束才回来执行原线程。
2.5线程优先级
static int ---------MAX_PRIORITY
线程可以拥有的最大优先级。
static int ---------MIN_PRIORITY
线程可以拥有的最小优先级。
static int -------- NORM_PRIORITY
被分配给线程的默认优先级。
void setPriority(int newPriority)
更改此线程的优先级。
int getPriority()
class MyThread implements Runnable{ // 实现Runnable接口
public void run(){ // 覆写run()方法
for(int i=0;i<5;i++){
try{
Thread.sleep(500) ; // 线程休眠
}catch(InterruptedException e){
}
System.out.println(Thread.currentThread().getName()
+ "运行,i = " + i) ; // 取得当前线程的名字
}
}
};
public class ThreadPriorityDemo{
public static void main(String args[]){
Thread t1 = new Thread(new MyThread(),"线程A") ; // 实例化线程对象
Thread t2 = new Thread(new MyThread(),"线程B") ; // 实例化线程对象
Thread t3 = new Thread(new MyThread(),"线程C") ; // 实例化线程对象
t1.setPriority(Thread.MIN_PRIORITY) ; // 优先级最低
t2.setPriority(Thread.MAX_PRIORITY) ; // 优先级最高
t3.setPriority(Thread.NORM_PRIORITY) ; // 优先级最中等
t1.start() ; // 启动线程
t2.start() ; // 启动线程
t3.start() ; // 启动线程
}
};
返回此线程的优先级。
2.6守护线程
God god = new God();
You you = new You();
Thread thread = new Thread(god);
thread.setDaemon(true); // 默认的是false,表示的是用户线程,正常的都是用户线程
thread.start(); // 上帝守护线程启动
new Thread(you).start();
3.线程状态
4.线程安全
4.1线程同步
线程同步:多个需要同时访问此对象的线程进入这个对象的等待池形成队列,等待前面线程使用完毕,下一个线程再使用。
同步方法:使用synchronized修饰的方法,保证当前线程在执行该方法的时候,其它线程只能在方法外排队。
同步代码块:synchronized关键字用于方法中的某个区块,表示只对这个区块的资源实行互斥访问。
格式:
synchronized (同步锁){
需要同步的代码块
}
三个不安全的例子:
1.不安全的买票
package demo3;
//不安全的买票
public class UnsafeBuyTiket {
public static void main(String[] args) {
// TODO Auto-generated method stub
BuyTicket station = new BuyTicket();
new Thread(station,"苦逼的我").start();
new Thread(station,"牛逼的大家").start();
new Thread(station,"可恶的黄牛").start();
}
}
class BuyTicket implements Runnable{
//票
private int ticketNums = 10;
boolean flag = true;//外部停止方式
@Override
public void run() {
//买票
while(flag)
{
try{
buy();
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
private void buy() throws InterruptedException{
//判断是否有票
if(ticketNums<=0)
{
return;
}
//模拟延时
Thread.sleep(100);
//买票
System.out.println(Thread.currentThread().getName()+"拿到了"+ticketNums--+"票");
}
}
运行结果(出现-1张票):
同步方法及同步块(synchronized)(把不安全的改成安全的)
package demo3;
//不安全的买票
public class UnsafeBuyTiket {
public static void main(String[] args) {
// TODO Auto-generated method stub
BuyTicket station = new BuyTicket();
new Thread(station,"苦逼的我").start();
new Thread(station,"牛逼的大家").start();
new Thread(station,"可恶的黄牛").start();
}
}
class BuyTicket implements Runnable{
//票
private int ticketNums = 10;
boolean flag = true;//外部停止方式
@Override
public void run() {
//买票
while(flag)
{
try{
buy();
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
//同步方法,锁的是this
private synchronized void buy() throws InterruptedException{
//判断是否有票
if(ticketNums<=0)
{
return;
}
//模拟延时
Thread.sleep(100);
//买票
System.out.println(Thread.currentThread().getName()+"拿到了"+ticketNums--+"票");
}
}
运行结果(-1没了):
2.不安全的取钱
package demo3;
//不安全的取钱
//两个人去银行取钱,账户
public class UnsafeBank {
public static void main(String[] args) {
// TODO Auto-generated method stub
Account a = new Account(100,"结婚基金");
Drawing you = new Drawing(a,50,"你");
Drawing girl = new Drawing(a,100,"girl");
you.start();
girl.start();
}
}
//账户
class Account{
int money;//余额
String name;//卡名
public Account(int money,String name)
{
this.money = money;
this.name = name;
}
}
//银行:模拟取款
class Drawing extends Thread{
Account account;//账户
int drawingMoney;//取了多少钱
int nowMoney;//现在手里有多少钱
public Drawing(Account account,int drawingMoney,String name) {
super(name);
this.account = account;
this.drawingMoney = drawingMoney;
}
//取钱
@Override
public void run() {
//判断有没有钱
if(account.money-drawingMoney<0)
{ System.out.println(Thread.currentThread().getName()+"钱不够");
return;
}
//sleep可以放大问题的发生性
try {
Thread.sleep(1000);
}catch(Exception e)
{
e.printStackTrace();
}
//卡内余额=余额-你取的钱
account.money = account.money-drawingMoney;
//你手里的钱
nowMoney = nowMoney + drawingMoney;
System.out.println(account.name+"余额为"+account.money);
//这两个操作等价 //System.out.println(this.getName()+Thread.currentThread().getName());
System.out.println(this.getName()+"手里的钱"+nowMoney);
}
}
运行结果(存款为负数):
package demo3;
//不安全的取钱
//两个人去银行取钱,账户
public class UnsafeBank {
public static void main(String[] args) {
// TODO Auto-generated method stub
Account a = new Account(100,"结婚基金");
Drawing you = new Drawing(a,50,"你");
Drawing girl = new Drawing(a,100,"girl");
you.start();
girl.start();
}
}
//账户
class Account{
int money;//余额
String name;//卡名
public Account(int money,String name)
{
this.money = money;
this.name = name;
}
}
//银行:模拟取款
class Drawing extends Thread{
Account account;//账户
int drawingMoney;//取了多少钱
int nowMoney;//现在手里有多少钱
public Drawing(Account account,int drawingMoney,String name) {
super(name);
this.account = account;
this.drawingMoney = drawingMoney;
}
//取钱
@Override
public void run() {
synchronized(account)
{
//判断有没有钱
if(account.money-drawingMoney<0)
{
System.out.println(Thread.currentThread().getName()+"钱不够");
return;
}
//sleep可以放大问题的发生性
try {
Thread.sleep(1000);
}catch(Exception e)
{
e.printStackTrace();
}
//卡内余额=余额-你取的钱
account.money = account.money-drawingMoney;
//你手里的钱
nowMoney = nowMoney + drawingMoney;
System.out.println(account.name+"余额为"+account.money);
//这两个操作等价
//System.out.println(this.getName()+Thread.currentThread().getName());
System.out.println(this.getName()+"手里的钱"+nowMoney);
}
}
}
运行结果(存款不会为负数了):
3.线程不安全的集合
package demo3;
import java.util.ArrayList;
import java.util.List;
//线程不安全的集合
public class UnsafeList {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
for(int i=0;i<10000;i++)
{
new Thread(()->{
list.add(Thread.currentThread().getName());
}).start();
}
try {
Thread.sleep(3000);
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println(list.size());
}
}
运行结果(不足10000,因为存在两个线程同时看到一个标志,于是name就被覆盖掉了):
package demo3;
import java.util.ArrayList;
import java.util.List;
//线程不安全的集合
public class UnsafeList {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
for(int i=0;i<10000;i++)
{
new Thread(()->{
synchronized(list)
{
list.add(Thread.currentThread().getName());
}
}).start();
}
try {
Thread.sleep(3000);
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println(list.size());
}
}
运行结果(list.size()=10000):
4.2Lock锁
4.3synchronized和Lock锁的比较
5.线程池
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestPool {
public static void main(String[] args) {
// 1.创建服务,创建线程池
ExecutorService service = Executors.newFixedThreadPool(10);
// 执行
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
// 2.关闭连接
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
运行结果:
6.线程间通信
概念:多个线程在处理同一个资源,但是处理的动作却不相同。
等待唤醒机制:多个线程间的协作机制。
wait():线程不再活动,不再参与调度。
notify():选取所通知对象的wait set 中的一个线程释放。
notifyall():释放所通知对象的wait set 中的全部线程。