进程:正在执行中的程序。
每一个进程执行都有一个执行顺序,该顺序是一个执行路径,
或者叫控制单元。
线程:就是进程中的一个独立的控制单元。
线程在控制着进程的执行。
一个进程中至少有一个线程。
static Thread currentThread():获取当前线程对象。
getName(); 获取线程名称。
设置线程名称:setName()或者构造函数。
扩展:JVM启动的时候不仅仅只有一个主线程,还有负责垃圾回收的线程(当主
线程执行的时候,存在于堆中的无用对象会被回收)
//创建线程的第一个方法:
1.继承Thread
2.复写Thread类中的run方法
3.调用线程的start方法
该方法有两个作用:启动线程,调用run方法
class Demo extends Thread{
public void run(){
System.out.println("demo run");
}
}
class ThreadDemo{
public static void main(String[] args){
Demo d=new Demo();//创建好一个线程
d.start();
}
}
//创建线程的第二种方法
步骤:
1.定义类实现Runnable接口
2.覆盖Runnable接口的run方法
将线程要运行的代码存放在该run方法中
3.通过Thread类建立线程对象。
4.将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数。
5.调用Thread类的start方法开启线程并调用Runnable接口子类的run方法。
class Ticket implements Runnable{
private 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();
}
}
------------------------------------------------------------------------------------------------------------
两种创建线程的方式区别:
1.实现方式好处:避免了单继承的局限性,在定义线程时,建议用实现方式。
2.继承Thread:线程代码存放在Thread子类run方法中。实现Runnable,线程代码存在接口的子类的run方法。
------------------------------------------------------------------------------------------------------------
Java对于多线程的安全问题提供了专业的解决方法。
就是同步代码块
synchronized(对象){ //哪些代码操作共享数据就同步哪些代码。
需要被同步的代码
}
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()+".......sale:"+tick--);
}
}
}
}
}
同步的前提:
1.必须要有两个或者两个以上的线程。
2.必须是多个线程使用同一个锁。
必须保证同步中智能有一个线程在运行。
好处:解决了多线程的安全问题。
弊端:多个线程都需要判断锁,较为消耗资源。
----------------------------------------------------------------------
/*多线程-同步函数
需求:
银行有一个金库,有两个储户分别存300,每次存100,存3次。
目的:该程序是否有安全问题,如果有,如何解决。*/
class Bank{
private int sum;
//Object obj=new Object();
public synchronized void add(int n){
//synchronized
sum=sum+n;
try{Thread.sleep(10);}catch(Exception e){}
System.out.println("sum="+sum);
}
}
calss Cus implements Runnable{
private Bank b=new Bank();
public void run(){
for (int x=0;x<3;x++ ) {
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 该对象的类型是Class。
*/
class Ticket implements Runnable{
private int tick=1000;
public void run(){
while(true){
show();
}
}
public synchronized void show(){
if (tick>0) {
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+".....sale:"+tick--);
}
}
}
class ThisLockDemo{
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);
}
}
----------------------------------------------------------
多线程-单例设计模式
饿汉式:
class Single{
private static Single s=new Single();
private Single(){};
public static Single getInstance(){
return s;
}
}
懒汉式: 实例延迟加载,如果多线程访问的时候会出现安全问题。
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(MyLock.locka){
System.out.println("if locka");
synchronized(MyLock.lockb){
System.out.println("if lockb");
}
}
}else{
synchronized(MyLock.lockb){
System.out.println("else lockb");
synchronized(MyLock.locka){
System.out.println("else locka");
}
}
}
}
}
class MyLock{
static object locka=new object();
static object lockb=new object();
}
class DeadLockTest{
public static void main(String[] args){
Thread t1=new Thread(new Test(true));
Thread t2=new Thread(new Test(false));
t1.start();
t2.start();
}
}