进程:是一个正在执行中的程序。
每一个进程执行都有一个执行顺序,该顺序是一个执行路径,或者叫一个控制单元
线程:就是一个独立的控制单元。
线程在控制着进程的执行。
一个进程中至少有一个线程;
java VM启动的时候回有一个进程java.exe.
该进程中至少有一个线程负责Java程序的执行。
而且这个线程的代码存在于main方法中 ,该线程称之为主线程
扩展: 其实更细节说明JVM,启动时不只一个线程,还有垃圾回收机制的线程;
1:如何在自定义的代码中自定义线程呢?
通过对API的查找,Java已经对线程这类事物的描述,就Thread类。
创建线程的第一种方式,继承Thread类;
步骤:
1:定义类继承Thread
2:复写Thread类中run方法
目的:将自定义的代码存储在run方法。让线程运行
3:调用线程的start方法。
该方法有两个作用:启动线程,调用run方法
为什么要覆盖run方法?
Thread类用于描述线程。
该类就定义了一个功能,用于存储线程要运行的代码。该存储功能就是run方法、
也就是说Thread类中run方法中,用来存储线程要运行的代码。
[java] view plaincopy
- class Demo extends Thread
- {
- public void run()
- {
- for(int i=0;i<10;i++)
- System.out.println("demo...."+i);
- }
- }
- class XianchengDemo2
- {
- public static void main(String[] args)
- {
- Demo d=new Demo();
- d.start();
- //d.run(); 若这样只会调用run方法,并没有启动线程
- for(int x=0;x<10;x++)
- System.out.println("main...."+x);
- }
- }
线程都有自己默认的名称;
Thread-编号,该编号从0开始;
static Thread currentThread():获取当前线程对象。
getName():获取线程的名称;
设置线程名称:setName或者构造函数。
[java] view plaincopy
- class Demo extends Thread
- {
- Demo(String name)
- {
- super(name);
- }
- public void run()
- {
- for(int i=0;i<50;i++)
- System.out.println("name"+Thread.currentThread().getName()+"...."+i);
- }
- }
- class XiancDemo
- {
- public static void main(String[] args)
- {
- Demo d=new Demo("one+++++");
- Demo e=new Demo("two-----");
- d.start();
- e.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方法;
练习:
需求:简单的买票程序。
多个窗口同时买票;
[java] view plaincopy
- */
- class Ticket implements Runnable
- {
- private int tick=100;
- public void run()
- {
- while(true)//这是个无限循环,所以在DOS窗口我们要使用Ctrl+c 来停止循环;
- {
- if(tick>0)
- {
- System.out.println(Thread.currentThread().getName()+"..."+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;
- Thread(String name)
- {
- super(name);
- }
- public void run()
- {
- while(true)
- {
- if(tick>0)
- {
- System.out.println(Thread.currentThread().getName()+"..."+tick--);
- }
- }
- }
- }
- class TicketDemo
- {
- public static void main(String[] args)
- {
- Thread t1=new Thread(t1);
- Thread t2=new Thread(t2);
- Thread t3=new Thread(t3);
- Thread t4=new Thread(t4);
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
- */
同步函数中用的是哪一个锁呢?
函数需要被对象调用。那么函数都有一个所属对象的引用,就是this;
所以同步函数的锁是this;
多线程的安全问题:
通过分析,发现,打印出0,-1,-2等错票;
多线程的运行出现了安全问题。
[java] view plaincopy
- class Ticket implements Runnable
- {
- private int tick=100;
- public void run()
- {
- while(true)
- {
- if(tick>0)
- {
- try{Thread.sleep(10);}catch(Exception e){}//测试有无错误
- System.out.println(Thread.currentThread().getName()+"..."+tick--);
- }
- }
- }
- }
- class TicketDemo3
- {
- 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();
- }
- }
问题的原因:
当多条语句在操作同一个线程共享数据时,一个线程对多条语句只执行了一部分,
还没有执行完,另一个线程参与进来执行。导致共享数据的错误
解决办法:
对多条操作共享数据的语句,只能让一个线程执行完。在执行过程中,其他线程不可参与执行
Java对于多线程的安全问题提供了专业的解决方式。就是同步代码块
synchronized(对象)
{
需要同步的代码;
}
对象如同锁。持有锁的线程可以在同步中执行
没有持有锁的线程即使获取CPU的执行权,也进不去,因为没有获取锁
同步前提:
1:必须要有两个或以上的线程;
2:必须是多个线程使用同一个锁。
好处:解决多线程的安全问题;
弊端:多个线程都需要判断锁,较为消耗资源,
[java] view plaincopy
- class Ticket implements Runnable
- {
- private 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()+"..."+tick--);
- }
- }
- }
- }
- }
- class TicketDemo3
- {
- 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();
- }
- }
再看一个
需求:
银行有个金库
有两个存户,分别存300元,存三次每次存一百;
[java] view plaincopy
- class Bank
- {
- private int sum;
- public void add(int n)
- {
- sum+=n;
- System.out.println("sum="+sum);
- }
- }
- class Cus implements Runnable
- {
- private Bank b=new Bank();
- public void run()
- {
- for(int x=0;x<3;x++)
- b.add(100);
- }
- }
- class TongBuDemo
- {
- public static void main(String[] args)
- {
- Cus c=new Cus();
- Thread t1=new Thread(c);
- Thread t2=new Thread(c);
- t1.start();
- t2.start();
- }
- }
目的:该程序是否有安全问题?如果有,怎么解决?
如何找问题:
1,明确哪些代码是多线程运行代码;
2,明确共享数据;
3,明确多线程运行代码中哪些语句是操作共享数据的。
[java] view plaincopy
- class Bank
- {
- private int sum;
- public void add(int n)
- {
- sum+=n;
- try{Thread.sleep(10);}catch(Exception e){}//测试有无错误
- System.out.println("sum="+sum);
- }
- }
- class Cus implements Runnable
- {
- private Bank b=new Bank();
- public void run()
- {
- for(int x=0;x<3;x++)
- b.add(100);
- }
- }
- class TongBuDemo
- {
- public static void main(String[] args)
- {
- Cus c=new Cus();
- Thread t1=new Thread(c);
- Thread t2=new Thread(c);
- t1.start();
- t2.start();
- }
- }
没有出现300;而出现了两次200;
[java] view plaincopy
- class Bank
- {
- private int sum;
- Object obj=new Object();
- public void add(int n)
- {
- synchronized(obj)
- {
- sum+=n;
- try{Thread.sleep(10);}catch(Exception e){}//测试有无错误
- System.out.println("sum="+sum);
- }
- }
- }
- class Cus implements Runnable
- {
- private Bank b=new Bank();
- public void run()
- {
- for(int x=0;x<3;x++)
- b.add(100);
- }
- }
- class TongBuDemo
- {
- public static void main(String[] args)
- {
- Cus c=new Cus();
- Thread t1=new Thread(c);
- Thread t2=new Thread(c);
- t1.start();
- t2.start();
- }
- }
大家有没有注意到
同步代码块是用来封装代码的,add函数是不是也是封装代码的;
如果让函数具备同步性呢;
[java] view plaincopy
- class Bank
- {
- private int sum;
- //Object obj=new Object();
- public synchronized void add(int n)
- {
- //synchronized(obj)
- //{
- sum+=n;
- try{Thread.sleep(10);}catch(Exception e){}//测试有无错误
- System.out.println("sum="+sum);
- //}
- }
- }
- class Cus implements Runnable
- {
- private Bank b=new Bank();
- public void run()
- {
- for(int x=0;x<3;x++)
- b.add(100);
- }
- }
- class TongBuDemo
- {
- public static void main(String[] args)
- {
- Cus c=new Cus();
- Thread t1=new Thread(c);
- Thread t2=new Thread(c);
- t1.start();
- t2.start();
- }
- }
这个效果是一样的