一、多线程(概述)
进程:是一个正在执行中的程序。每一个进程执行都有一个执行的顺序,该顺序就是一个执行路径,或者叫控制单元。
线程:是进程中的一个独立的控制单元。线程在控制着进程的执行。
一个进程中至少有一个线程。
Java VM启动的时候会有一个进程java.exe。该进程中至少有一个线程负责java程序的执行,而且这个线程运行的代码存在于main方法中,该线程称为主线程。
扩展:其实JVM启动不止一个线程,还有负责垃圾回收机制的线程。
二、多线程(创建线程-继承Thread类)
通过API查找,Java已经提供了对线程这类事物的描述,就是Thread类。
创建线程的第一种方式:继承Thread类。
1.定义类继承Thread类。
2.复写Thread类中的run方法。
3.调用线程的start方法,该方法有两个作用,第一是启动线程,第二是调用run方法。
- /*
- 测试:
- */
- class Demo extends Thread
- {
- public void run()
- {
- for(int i=0;i<60;i++)
- System.out.println("demo run");
- }
- }
- class ThreadDemo
- {
- public static void main(String[] args)
- {
- Demo d=new Demo();//创建好一个线程。
- d.start();
- for(int i=0;i<60;i++)
- System.out.println("min run");
- }
- }
发现运行结果每次都不同,因为多个线程都在获取CPU的执行权,CPU执行到谁,谁就运行。明确一点,在某一时刻,只能有一个程序在运行。CPU在做着快速的切换,以达到看上去是同时运行的效果。
我们可以形象的把多线程的运行行为看作是在互相抢夺CPU的执行权,这就是多线程的一个特性:随机性。谁抢到谁执行,至于执行多长时间,CPU说的算。
三、多线程(创建线程-run和start特点)
为什么要复写run方法?
Thread类用于描述线程。该类就定义了一个功能,用于存储线程要执行的代码,该存储功能就是run方法。也就是说,Thread 类中的run方法用于存储线程要执行的代码。
所以,复写run方法的目的是将自定义的代码存储在run方法中,让线程运行。
start:开启线程,执行run方法。
run:仅仅是对象调用方法。虽然线程创建了,但没执行。
四、多线程(线程练习)
- /*
- 练习:创建两个线程和主线程交替运行。
- */
- class Test extends Thread
- {
- private String name;
- Test(String name)
- {
- this.name=name;
- }
- public void run()
- {
- for(int i=0;i<60;i++)
- {
- System.out.println(name+" run..."+i);
- }
- }
- }
- class TreadTest
- {
- public static void main(String[] args)
- {
- Test t1=new Test("one");
- Test t2=new Test("two");
- t1.start();
- t2.start();
- for(int i=0;i<60;i++)
- {
- System.out.println("main run..."+i);
- }
- }
- }
五、多线程(线程运行状态)
六、多线程(获取线程对象以及名称)
线程都有自己的默认的名称:Thread-编号 该编号从0开始。通过Thread类的getName方法获取。
static Thread currentThread();//获取当前线程对象。
getName();//获取线程名称
设置线程名称:setName或者构造函数。
七、多线程(售票的例子)
- /*
- 需求:简单的卖票程序
- 多个窗口同时卖票
- */
- class Ticket extends Thread
- {
- private static int tick=100;
- public void run()
- {
- while(true)
- {
- if(tick>0)
- {
- System.out.println(currentThread().getName()+"...sale:"+tick--);
- }
- }
- }
- }
- class TicketDemo
- {
- public static void main(String[] args)
- {
- Ticket t1=new Ticket();
- //Ticket t2=new Ticket();
- //Ticket t3=new Ticket();
- //Ticket t4=new Ticket();
- t1.start();
- t1.start();
- t1.start();
- t1.start();
- }
- }
八、多线程(创建线程-实现Runnable接口)
创建线程的第二种方式:
1.定义类实现Runnable接口。
2.复写Runnable接口中的run方法。(将线程要运行的代码存放在该run方法中)
3.通过Thread类建立线程对象。
4.将Runnable接口的子对象作为实际参数传递给Thread类的构造函数。(为什么要将Runnable接口的子类对象传递给Thread类的构造函数?因为自定义的run方法所属的对象是Runnable接口的子类对象,所以要让线程去指定对象的run方法,就必须明确该run方法所属的对象)
5.调用Thread类的start方法开启线程并调用Runnable接口子类的run方法。
实现方式和继承方式有什么区别?
实现方式好处:避免了单继承的局限性。在定义线程时,建议使用实现方式。
两种方式的区别:
继承Thread类:线程代码存放在Thread子类run方法中。
实现Runnable接口:线程代码存在接口的子类的run方法中。
- /*
- 实现Runnable接口:
- */
- class Ticket implements Runnable
- {
- private static int tick=100;
- public void run()
- {
- while(true)
- {
- if(tick>0)
- {
- System.out.println(Thread.currentThread().getName()+"...sale:"+tick--);
- }
- }
- }
- }
- class TicketDemo
- {
- public static void main(String[] args)
- {
- Ticket t=new Ticket();
- Thread t1=new Thread(t);
- Thread t2=new Thread(t);
- Thread t3=new Thread(t);
- Thread t4=new Thread(t);
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
九、多线程(线程的安全问题)
情景模拟:
- /*
- 需求:简单的卖票程序
- 多个窗口同时卖票
- */
- class Ticket extends Thread
- {
- private static int tick=100;
- public void run()
- {
- while(true)
- {
- if(tick>0)
- {
- try
- {
- Thread.sleep(100);
- }
- catch (Exception e)
- {
- }
- System.out.println(Thread.currentThread().getName()+"...sale:"+tick--);
- }
- }
- }
- }
- class TicketDemo
- {
- public static void main(String[] args)
- {
- //Ticket t=new Ticket();
- Ticket t1=new Ticket();
- Ticket t2=new Ticket();
- Ticket t3=new Ticket();
- Ticket t4=new Ticket();
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
多线程的运行出现了安全问题。
问题的原因:
当多条语句在操作同一个线程共享数据时,一个线程对多条语句只执行了一部分,还没有执行完,另一个线程参与进来执行,导致共享数据的错误。
解决方法:
对多条操作共享数据的语句,只能让一个线程都执行完,在执行过程中,其它线程不可以参与执行。
Java对于多线程的安全问题提供了同步代码块来解决。
同步代码块:
synchronized(对象)
{
需要被同步的代码
}
- /*
- 需求:简单的卖票程序
- 多个窗口同时卖票
- */
- class Ticket extends Thread
- {
- private static int tick=100;
- Object obj=new Object();
- public void run()
- {
- while(true)
- {
- synchronized(obj)
- {
- if(tick>0)
- {
- try
- {
- Thread.sleep(10);
- }
- catch (Exception e)
- {
- }
- System.out.println(Thread.currentThread().getName()+"...sale:"+tick--);
- }
- }
- }
- }
- }
- class TicketDemo
- {
- public static void main(String[] args)
- {
- //Ticket t=new Ticket();
- Ticket t1=new Ticket();
- Ticket t2=new Ticket();
- Ticket t3=new Ticket();
- Ticket t4=new Ticket();
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
十、多线程(多线程-同步代码块)
- synchronized(对象)
- {
- 需要被同步的代码
- }
对象如同锁,持有锁的线程可以在同步中执行。没有持有锁的线程即使获取CPU的执行权,也进不去,因为没有获取锁。
同步的前提:
1.必须要有两个或两个以上的线程。
2.必须是多个线程使用同一个锁。
必须保证同步中只能有一个线程在运行。
好处:解决多线程的安全问题。
弊端:多个线程需要判断锁,较为消耗资源。
十一、多线程(多线程-同步函数)
- /*
- 需求:
- 银行有一个金库。有两个储户分别存300元,每次存100,存3次。
- 目的:该程序是否有安全问题?如果有,怎么解决?
- 如何找问题:
- 1.明确哪些代码是多线程代码
- 2.明确共享数据
- 2.明确多线程运行代码中哪些语句是操作共享数据的。
- */
- class Bank
- {
- private int sum;
- public synchronized void add(int n)
- {
- sum=sum+n;
- try
- {
- Thread.sleep(1000);
- }
- catch (Exception e)
- {
- }
- System.out.println("sum="+sum);
- }
- }
- class Cus implements Runnable
- {
- private Bank b=new Bank();
- public void run()
- {
- for(int i=0;i<3;i++)
- {
- b.add(100);
- }
- }
- }
- class BankDemo
- {
- public static void main(String[] args)
- {
- Cus c=new Cus();
- Thread t1=new Thread(c);
- Thread t2=new Thread(c);
- t1.start();
- t2.start();
- }
- }
十二、多线程(多线程-同步函数的锁是this)
函数需要被对象调用,那么函数都有一个所属对象的引用,就是this。
- /*
- 同步函数用的是哪个锁呢?
- 函数需要被对象调用,那么函数都有一个所属对象的引用,就是this。
- 所以,同步函数使用的锁是this。
- 通过该程序进行验证。
- 使用两个线程来卖票:
- 一个线程在同步代码块中。
- 一个线程在同步函数中。
- 都在执行卖票动作。
- */
- class Ticket implements Runnable
- {
- private static int tick=50;
- //Object obj=new Object();
- boolean flag=true;
- public void run()
- {
- if(flag)
- {
- while(true)
- {
- synchronized(this)
- {
- if(tick>0)
- {
- try{Thread.sleep(10);}catch (Exception e){}
- System.out.println(Thread.currentThread().getName()+"...synchronized:"+tick--);
- }
- }
- }
- }
- else
- while(true)
- {
- this.show();
- }
- }
- public synchronized void show()//this
- {
- if(tick>0)
- {
- try{Thread.sleep(10);}catch (Exception e){}
- System.out.println(Thread.currentThread().getName()+"...show:"+tick--);
- }
- }
- }
- class TicketDemo
- {
- public static void main(String[] args)
- {
- Ticket t=new Ticket();
- Thread t1=new Thread(t);
- Thread t2=new Thread(t);
- t1.start();
- try{Thread.sleep(10);}catch (Exception e){}
- t.flag=false;
- t2.start();
- }
- }
十三、多线程(多线程-静态同步函数的锁是Class对象)
静态的同步方法,使用的锁是该方法所在类的字节码文件对象。类名.class
- /*
- 如果同步函数被静态修饰后,使用的锁是什么?
- 通过验证,发现不是this。因为静态方法中不可以使用this。
- 静态进内存时,内存中没有本类对象,但一定有该类对应的字节码文件对象。
- 类名.class。该对象的类型是Class
- 静态的同步方法,使用的锁是该方法所在类的字节码文件对象。类名.class
- */
- class Ticket implements Runnable
- {
- private static int tick=50;
- boolean flag=true;
- public void run()
- {
- if(flag)
- {
- while(true)
- {
- synchronized(Ticket.class)
- {
- if(tick>0)
- {
- try{Thread.sleep(10);}catch (Exception e){}
- System.out.println(Thread.currentThread().getName()+"...synchronized:"+tick--);
- }
- }
- }
- }
- else
- while(true)
- {
- this.show();
- }
- }
- public static synchronized void show()//Ticket.class
- {
- if(tick>0)
- {
- try{Thread.sleep(10);}catch (Exception e){}
- System.out.println(Thread.currentThread().getName()+"...show:"+tick--);
- }
- }
- }
- class TicketDemo
- {
- public static void main(String[] args)
- {
- Ticket t=new Ticket();
- Thread t1=new Thread(t);
- Thread t2=new Thread(t);
- t1.start();
- try{Thread.sleep(10);}catch (Exception e){}
- t.flag=false;
- t2.start();
- }
- }
十四、多线程(多线程-单例设计模式-懒汉式)
- /*
- 单例设计模式:
- 饿汉式:
- class Single
- {
- private static final Single s=new Single();
- private Single(){}
- public static Single getInstance()
- {
- return s;
- }
- }
- */
- //懒汉式:
- /*
- 懒汉式和饿汉式有什么不同?
- 懒汉式的特点在于延迟实例的加载。
- 有没有问题?
- 如果多线程访问会出现安全问题。
- 怎么解决?
- 加同步。同步代码块和同步函数都可以解决,但稍微低效。可以通过
- 双重判断解决效率问题。
- 加同步使用的锁是哪个?
- 该类所属的字节码文件对象。类名.class
- */
- class Single
- {
- private static Single s=null;
- private Single(){};
- public static Single getInstance()
- {
- if(s==null)
- {
- synchronized(Single.class)
- {
- if(s==null)
- {
- s=new Single();
- }
- }
- }
- return s;
- }
- }
- /*
- 死锁:
- */
- class Test implements Runnable
- {
- private boolean flag;
- Test(boolean flag)
- {
- this.flag=flag;
- }
- public void run()
- {
- if(flag)
- {
- synchronized(MyLook.looka)
- {
- System.out.println("if looka");
- synchronized(MyLook.lookb)
- {
- System.out.println("if lookb");
- }
- }
- }
- else
- {
- synchronized(MyLook.lookb)
- {
- System.out.println("else lookb");
- synchronized(MyLook.looka)
- {
- System.out.println("else looka");
- }
- }
- }
- }
- }
- class MyLook
- {
- static Object looka=new Object();
- static Object lookb=new Object();
- }
- class DeadLockDemo
- {
- public static void main(String[] args)
- {
- Thread t1=new Thread(new Test(true));
- Thread t2=new Thread(new Test(false));
- t1.start();
- t2.start();
- }
- }