概述
进程:正在进行中的程序
线程:就是进程中的一个负责程序执行的控制单元(执行路径)
多线程:一个进程中可以多执行路径
一个进程中至少有一个线程。开启多个线程是为了同时运行多部分代码。
每一个线程都有自己运行的内容,这个内容可以称为线程要执行的任务。
多线程好处:解决了多部分同时运行的问题
多线程的弊端:线程太多回到效率的降低
其实应用程序的执行都是cpu在做着快速的切换完成的,这个切换是随机的。
JVM启动时就启动了多个线程,至少有两个线程可以分析的出来。
1.执行main函数的线程:该线程的任务代码都定义在main函数中。
2.负责垃圾回收的线程。
package com.monfolld;
class DemoT extends Object{
public void finalize(){
System.out.println("deno ok");
}
}
public class ThreadDemo {
public static void main(String[] args){
new DemoT();
new DemoT();
new DemoT();
System.gc(); //垃圾回收
System.out.println("hello world");
}
}
/*
运行结果(不唯一):
deno ok
deno ok
deno ok
hello world
*/
多线程的创建方式
创建线程的目的是为了开启一条执行路径,去运行指定的代码和其他代码实现同时运行,而运行的指定代码就是这个执行路径的任务, jvm创建的主线程任务都定义在了主函数中,而定义的线程他的任务在哪呢?
thread类用于描述线程,线程是需要任务的,所以thread类也对任务的描述。这个任务就通过thread类中的run方法来体现,也就是说,run方法就是封装自定义线程运行任务的函数,run方法中定义就是线程要运行的任务代码。
开启线程是为了运行指定代码,所以只有继承thread类,并复写run方法。将运行的代码定义在run方法中即可。
步骤:
1.定义一个类继承Thread类
2.覆盖Thread类中的run方法
3.直接创建Thread的子类对象创建线程
4.调用start方法开启线程并调用线程的任务run方法执行
package com.monfolld;
class DemoT2 extends Thread{ //创建一个类继承Thread
private String name;
DemoT2(String name){
this.name=name;
}
public void run(){
for (int x=0;x<10;x++){
for (int y=0;y<999;y++){
System.out.println(name+"....x"+x+Thread.currentThread().getName());//可以调用getname获取线程的编号
}
}
}
}
public class ThreadDemo2 {
public static void main(String[] args){
DemoT2 d1=new DemoT2("旺财");
DemoT2 d2=new DemoT2("xiaoqiang");
d1.start(); //start方法开启线程
d2.start();
}
}
运行内存图解
主线程一开启,就是一条执行路径,执行过程中,创建了线程,start开启路径,每个线程有自己独立的空间(栈)
当线程出项异常时,该线程结束,其他线程继续执行
线程的状态
CPU的执行资格:可以被cpu处理,在处理队列中排队
CPU的执行权:正在被cpu处理
创建线程的第二种方式--Runnable
1.定义类实现Runnable接口
2.覆盖接口中的run方法,将线程的任务代码封装到run方法中
3.通过Thread类创建线程对象,并将Runnable接口的子类对象作为Thread类的构造函数的参数进行传递,因为线程的任务都封装在Runnable接口子类对象的run方法中,所以要在线程对象创建时就必须明确要运行的任务
4.调用线程对象的start方法开启线程
实现Runnable接口的好处:
1.将线程的任务从线程的子类中分离出来,进行了单独的封装。按照面向对象的思想将任务封装成对象
2.避免了java单继承的局限性。
package com.monfolld;
//扩展Demo类的功能,让其中的内容可以作为线程的任务执行,通过Runnable接口来完成
public class DemoT3 implements Runnable{
public void run(){
show();
}
public void show(){
for (int x=0;x<20;x++){
System.out.println(Thread.currentThread().getName()+"..."+x);
}
}
}
class ThredaDemo{
public static void main(String[] args){
DemoT3 demoT3=new DemoT3();
Thread thread1=new Thread(demoT3);
Thread thread2=new Thread(demoT3);
thread1.start();
thread2.start();
}
}
多线程(买票示例)
对于一个买票窗口对象,创建四个任务线程进行。
package com.monfolld;
public class Ticket implements Runnable{
private int num=100;
public void run(){
while (true){
if (num>0){
System.out.println(Thread.currentThread().getName()+"sale..."+num--);
}
}
}
}
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();
}
}
但是这样做的话会有线程的安全问题,结果有可能产生-1,-2等。
线程安全问题
产生的原因:
1.多个线程在操作共享的数据
2.操作共享数据的线程代码有多条
当一个线程在执行操作共享数据的多条代码过程中,其他线程参与了运算,就会产生导致线程安全的问题。
解决思路:同步代码块
就是将多条操作共享数据的线程代码封装起来,当有线程在执行这些代码的时候,其他线程不可以参与运算的。必须要当前线程把这些代码都执行完以后,其他线程才可以参与运算。
在java中,用同步代码块就可以解决这个问题。
同步代码块格式:
synchronized(对象){
需要被同步的代码;
}
package com.monfolld;
public class Ticket implements Runnable{
private int num=100;
Object obj=new Object();
public void run(){
while (true){
synchronized (obj){//同步代码块
if (num>0){
try {
Thread.sleep(10);
}
catch (InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"sale..."+num--);
}
} }
}
}
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();
}
}
同步的好处:解决了线程的安全问题。
同步的弊端:相对降低了效率,因为同步外的线程的都会判断同步锁
同步的前提:必须有多个线程并使用同一个锁。
同步函数
格式:直接在函数前面加上synchronized修饰符
package com.monfolld;
public class Ticket implements Runnable{
private int num=100;
//Object obj=new Object();
public void run(){
while (true){
//synchronized (obj){//同步代码块
show(); //调用同步函数
} //}
}
public synchronized void show(){ //同步函数
if (num>0){
try {
Thread.sleep(10);
}
catch (InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"sale..."+num--);
}
}
}
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();
}
}
验证同步函数的锁
同步函数使用的锁时this;
同步函数和同步代码块的区别:
同步函数的锁是固定的this,同步代码块的锁是任意的对象,建议使用同步代码块。
当线程在同步代码块和同步函数同时运行时,可以用this将它们指向同一个锁,保证线程的安全性
package com.monfolld;
public class Ticket implements Runnable{
private int num=100;
boolean flag =true;
//Object obj=new Object();
public void run(){
if (flag){
while (true){
synchronized (this){//同步代码块
if (num>0){
try {
Thread.sleep(10);
}
catch (InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"obj..."+num--);
} //调用同步函数
} }}
else //调用同步函数
while (true){
this.show();
}
}
public synchronized void show(){ //同步函数
if (num>0){
try {
Thread.sleep(10);
}
catch (InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"function..."+num--);
}
}
}
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 (InterruptedException e){}
t.flag=false;
t2.start();
}
}
静态同步函数的锁
静态的同步函数使用的锁就是该函数所属字节码文件对象,可以用getClass方法获取,也可以当前类名.class表示。
package com.monfolld;
public class Ticket implements Runnable{
private static int num=100;
boolean flag =true;
//Object obj=new Object();
public void run(){
if (flag){
while (true){
synchronized (Ticket.class){//也可以this.getClass() //静态函数指向的锁this.getClass()
if (num>0){
try {
Thread.sleep(10);
}
catch (InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"obj..."+num--);
} //调用同步函数
} }}
else //调用同步函数
while (true){
this.show();
}
}
public static synchronized void show(){ //静态同步函数
if (num>0){
try {
Thread.sleep(10);
}
catch (InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"function..."+num--);
}
}
}
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 (InterruptedException e){}
t.flag=false;
t2.start();
}
}
单例设计模式涉及的多线程
饿汉式没毛病,但懒汉式设计cpu在切换时有多个线程进入s==null判断,然后线程阻塞会出现安全隐患,
于是就用同步函数或者同步代码块解决
package com.monfolld;
//没有对象,只有调用getInstance方法时,才会创建对象。
class Single {
private static Single s=null;
private Single(){}
public static Single getInstance() { //可以使用同步函数public static synchronized Single getInstance()
if (s == null) {
synchronized (Single.class){ //也可以使用同步代码块
if (s==null){
s = new Single();
}
}
}
return s;
}
}
class SingleDemo {
public static void main(String[] args){
Single ss=Single.getInstance();
}
}
死锁
常见的死锁之一:同步的嵌套
package com.monfolld;
class TestL implements Runnable{
private boolean flag;
TestL(boolean flag){
this.flag=flag;
}
public void run(){
if (flag){
while (true){
synchronized (MyLock.locka){
System.out.println("if locka");
synchronized (MyLock.lockb){
System.out.println("if lockb");
}
}}
}
else
{
while (true){
synchronized (MyLock.lockb){
System.out.println("else lockb");
synchronized (MyLock.locka){
System.out.println("else locka");
}
}
}
}}
}
class MyLock{
public static final Object locka=new Object();
public static final Object lockb=new Object();
}
public class DeadLockTest {
public static void main(String[] args){
TestL a=new TestL(true);
TestL b=new TestL(false);
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();
}
}