Java-多线程

Java—多线程

1.0进程与线程概念

进程就是程序的第一次动态过程,它需要经历从代码加载、代码执行到执行完毕的一个完整过程,这个过程也是进程本书从产生、发展到最后消亡的过程。

多线程是实现并发机制的一种有效的手段,进程和线程都一样,都是实现并发的一个基本单位,线程是比进程更小的执行单位,所谓的多线程是指一个进程在执行过程中可以产生多个更小的程序单元。

所谓的线程(Thread)是指程序的运行流程,多线程机制可以同时运行多个程序块。

1.1Java中线程的实现

1.1.0继承Thread类

一个类只要继承了Thread类,此类就称为多线程实现类,在Thread子类中必须重写Thread类的run()方法,此方法为线程的主体。

public class MyThread extends Thread
{
    private String name;

    public MyThread(String name) {
        this.name = name;
    }
    public void run()
    {
        for (int i = 0; i < 10; i++) {
            System.out.println(name+"运行,i="+i);
        }
    }
}

//主方法测试类
public class ThreadDemo01
{
    public static void main(String[] args) {
        MyThread mt1=new MyThread("线程A");
        MyThread mt2=new MyThread("线程B");
        mt1.run();
        mt2.run();
    }
}

此时根据运行结果,此线程实际上没有被启动,还是属于顺序执行的方式。

要正确启动线程,不能直接调用run()方法,而是应该调用Thread类中继承来的start()方法。

//只需要在上面代码中修改,把run()方法改为start()方法即可

mt1.start();
mt2.start();

总结:在线程启动时虽然调用的是start()方法,但实际确是调用了run()方法的主体。

如果一个类通过继承的Thread类来实现,那么只能调用一次start()方法,如果调用多次则会抛出异常。

public class MyThread extends Thread
{
    private String name;

    public MyThread(String name) {
        this.name = name;
    }
    public void run()
    {
        for (int i = 0; i < 10; i++) {
            System.out.println(name+"运行,i="+i);
        }
    }
}

//主方法测试类
public class ThreadDemo01
{
    public static void main(String[] args) {
        MyThread mt1=new MyThread("线程A");
        mt1.start();
        mt1.start();
    }
}

如果一个类只能继承Thread类才能实现多线程,则必定会受到单继承局限的影响。一般来说,要想实现多线程还可以通过实现Runnable接口完成。

1.1.1实现Runnable接口

在java中可以通过Runnable接口方式实现多线程

public class MyThread implements Runnable
{
    private String name;

    public MyThread(String name) {
        this.name = name;
    }
    public void run()
    {
        for (int i = 0; i < 10; i++) {
            System.out.println(name+"运行,i="+i);
        }
    }
}

在Runnable接口中没有start()方法定义,要启动多线程,可以在Thread类中提供public Thread(Runnable target)和public Thread(Runnable target,String name)两个构造方法来实现多线程,这两个构造方法都可以接收Runnable的子类实例对象。

public class ThreadDemo01
{
    public static void main(String[] args) {
        //实例化Runnable的两个子类对象
        MyThread my1=new MyThread("线程A");
        MyThread my2=new MyThread("线程B");
        //实例化Thread对象
        Thread t1=new Thread(my1);
        Thread t2=new Thread(my2);
        t1.start();
        t2.start();
    }
}

也就是说不管用那种方式,用Thread继承也好,Runnable接口也好,最终都必须依靠Thread类才能启动多线程。

1.1.2Thread类和Runnable联系

看原始定义

public class Thread extends Object implements Runnable

从定义可以发现,Thread类也是Runnable接口的子类,但是在Thread类中没有实现Runnable接口中的run()方法

也就是说此方法是由Runnable子类完成的,所以如果要通过继承Thread类实现多线程,则必须重写run()方法。

一个类继承Thread类,则并不适用多个线城共享资源,请看下面例子

public class MyThread extends  Thread
{
    private int ticket=5;
    public void run()
    {
        for (int i = 0; i < 100; i++) {
            if(ticket>0)
            {
                System.out.println("卖票:ticket="+ticket--);
            }
                    }
    }
}

public class ThreadDemo01
{
    public static void main(String[] args) {
        //实例化Runnable的两个子类对象
        MyThread my1=new MyThread();
        MyThread my2=new MyThread();
        MyThread my3=new MyThread();

        my1.start();
        my2.start();
        my3.start();
    }
}

程序启动了3给线程,但是3给线程却分别卖各自5张票,没有达到资源共享的目的。

实现Runnable接口可以资源共享

public class MyThread implements Runnable
{
    private int ticket=5;
    public void run()
    {
        for (int i = 0; i < 100; i++) {
            if(ticket>0)
            {
                System.out.println("卖票:ticket="+ticket--);
            }
                    }
    }
}

public class ThreadDemo01
{
    public static void main(String[] args) {
        //实例化Runnable的两个子类对象
        MyThread my1=new MyThread();
        new Thread(my1).start();
        new Thread(my1).start();
        new Thread(my1).start();
    }
}

从程序运行结果可以清除发现,虽然启动了3给线程,但是3给线程一共才执行了5张票,即ticket属性被所以的线程对象共享。

实现Runnable接口相对于继承Thread类来说,有一下的优势:

  • 适合多个相同的程序代码线程去处理统一资源情况
  • 可以避免由于java的单继承特性的局限性
  • 代码可以被多个线程共享,代码与数据是独立的

1.2线程的状态

  • 运行状态

简单说就是创建了一个线程对象后,新的线程对象处于新建状态,例如:Thread thread=new Thread()

  • 就绪状态

当线程启动时,线程就进入就绪状态,例如调用线程中的start()方法,此时线程进入队列排队,等待CPU的服务

  • 堵塞状态

当就绪状态的线程被调用获得处理起资源时,线程就进入了运行状态,此时自动调用run()方法,run方法定义了该线程的操作和功能。

  • 死亡状态

线程调用stop()方法或run()方法执行结束后,就处于死亡状态,不在具有继续运行能力。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xABs6FAe-1662722798149)(C:\Users\13417\AppData\Roaming\Typora\typora-user-images\image-20220909192407056.png)]

1.3获得和设置线程的名称

在Thread类中可以通过getName()方法获得线程的名称,还可以通过setName()方法设置线程的名称。需要注意的是如果没有设置名称,系统会自动分配名称,格式为:Thread-Xx

public class MyThread implements Runnable
{
   public void run()
   {
       for (int i = 0; i < 3; i++) {
           System.out.println(Thread.currentThread().getName()+"运行,i="+i);
       }
   }
}

public class ThreadDemo01
{
    public static void main(String[] args) {
        //实例化Runnable的两个子类对象
        MyThread my1=new MyThread();
       new Thread(my1).start();
       new  Thread(my1,"线程-A").start();
       new  Thread(my1,"线程-B").start();
        new Thread(my1).start();
        new Thread(my1).start();
    }
}

可以看出没有设置线程名称都是很有规律的

在看一个例子

public class MyThread implements Runnable
{
   public void run()
   {
       for (int i = 0; i < 3; i++) {
           System.out.println(Thread.currentThread().getName()+"运行,i="+i);
       }
   }
}

public class ThreadDemo01
{
    public static void main(String[] args) {
        //实例化Runnable的两个子类对象
        MyThread my1=new MyThread();
       new Thread(my1,"线程").start();
       my1.run();
    }
}

可以看出其实主方法也是一个线程,在java中所有的线程都是同时启动的,哪个线程占到了CPU资源,哪个线程就可以先运行。

1.4判断线程是否启动

在前面可以知道,通过Thread类中的start()方法通知CPU这个线程已经准备好启动,等待CPU分配资源,运行此线程,在java中可以使用isAlive()方法来测试线程是否仍然在运行。

public class MyThread implements Runnable
{
   public void run()
   {
       for (int i = 0; i < 3; i++) {
           System.out.println(Thread.currentThread().getName()+"运行->>>"+i);
       }
   }
}


public class ThreadDemo01
{
    public static void main(String[] args) {
        //实例化Runnable的两个子类对象
        MyThread my1=new MyThread();
        Thread t=new Thread(my1,"线程");
        System.out.println("线程开始执行前>>>"+ t.isAlive());
        t.start();
        System.out.println("线程开始执行后>>>"+t.isAlive());
        for (int i = 0; i < 3; i++) {
            System.out.println("main运行->>>"+ i);
        }
        System.out.println("代码执行后>>>"+t.isAlive());
    }
}

以上输出结果是不确定的,有可能到最后线程已经不存活了,但也有可能继续存活,这就要看哪个线程先执行完。

1.5线程的强制执行

在线程操作中,可以使用join()方法让一个线程强制执行,线程运行期间,其他线程无法运行,必须等待此线程完成之后才可以继续执行。

public class MyThread implements Runnable
{
   public void run()
   {
       for (int i = 0; i < 3; i++) {
           System.out.println(Thread.currentThread().getName()+"运行->>>"+i);
       }
   }
}

public class ThreadDemo01
{
    public static void main(String[] args) {
        //实例化Runnable的两个子类对象
        MyThread my1=new MyThread();
        Thread t=new Thread(my1,"线程");
        t.start();
        for (int i = 0; i < 50; i++)
        {
            if(i>10){
                try {
                    t.join();
                }catch ( Exception e){}
            }
            System.out.println("Main线程运行->>>"+i);
        }

    }
}

可以知道所有的输出都将由一个线程来完成,主线程必须等待这个线程。

1.6线程的休眠

在一个程序中允许一个线程暂时的休眠,直接使用Thread.sleep()方法即可实现休眠。

public class MyThread implements Runnable
{
   public void run()
   {
       for (int i = 0; i < 5; i++)
       {
           try
           {
               //休眠500ms
               Thread.sleep(500);
           }catch (Exception e){}
           System.out.println(Thread.currentThread().getName()+"运行,i="+i);
       }
   }
}

public class ThreadDemo01
{
    public static void main(String[] args) {
        //实例化Runnable的两个子类对象
        MyThread my1=new MyThread();
        new Thread(my1,"线程").start();
    }
}

在上面程序中每次输出都会间隔500ms达到了延迟操作的效果。

1.7中断线程

当一个线程运行时,另外一个线程可以直接通过interrupt()方法中断其运行状态。

public class MyThread implements Runnable
{
   public void run()
   {

       System.out.println("1、进入run方法");
       {
           try
           {
               //休眠500ms
               Thread.sleep(10000);
               System.out.println("2、已经完成休眠");
           }catch (Exception e)
           {
               System.out.println("3、休眠被终止");
               return;
           }
           System.out.println("4、run方法正常结束");
       }
   }
}


public class ThreadDemo01
{
    public static void main(String[] args) {
        //实例化Runnable的两个子类对象
        MyThread my1=new MyThread();
        Thread t = new Thread(my1,"线程");
        t.start();
        try
        {
            Thread.sleep(1200);
        }catch (Exception e)
        {
        }
        t.interrupt();

    }
}

1.8后台线程

在Java程序中,只要前台有一个线程在运行,整个java进程都不会消失,所以此时可以设置一个后台线程,这样即使java进程结束了,此后台线程依然会继续执行。使用setDaemom()方法

public class MyThread implements Runnable
{
   public void run()
   {
       while(true)
       {
           System.out.println(Thread.currentThread().getName()+"在运行");
       }
   }
}

public class ThreadDemo01
{
    public static void main(String[] args) {
        //实例化Runnable的两个子类对象
        MyThread my1=new MyThread();
        Thread t = new Thread(my1,"线程");
        t.setDaemon(true);
        t.start();
    }
}

尽管run()方法是死循环的方式,但是程序依然可以执行完,因为方法中死循环的线程操作已经设置为后台运行了。

1.9线程的优先级

在java的线程中使用setPriority()方法可以设置一个线程优先级,在Java线程中一共有三种优先级

public class MyThread implements Runnable
{
   public void run()
   {
       for (int i = 0; i < 5; i++) {
           try
           {
               Thread.sleep(500);
           }catch (Exception e)
           {

           }
           System.out.println(Thread.currentThread().getName()+"运行,i="+i);
       }
   }
}

public class ThreadDemo01
{
    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();
    }
}

线程根据其优先级的大小决定哪个线程会先运行,但是并非线程的优先级越高就一定会先执行,哪个线程先执行将由CPU调度决定。

2.0线程的礼让

在线程操作中,可以使用yield()方法将一个线程的操作暂时让给其他线程执行。

public class MyThread implements Runnable
{
   public void run()
   {
       for (int i = 0; i < 5; i++) {
           System.out.println(Thread.currentThread().getName()+"运行->>>"+i);
        if(i==3)
        {
            System.out.println("线程礼让:");
            Thread.currentThread().yield();
        }
       }
   }
}

public class ThreadDemo01
{
    public static void main(String[] args) {
        Thread t1=new Thread(new MyThread(),"线程A");
        Thread t2=new Thread(new MyThread(),"线程B");
        t1.start();
        t2.start();
    }
}

2.1线程操作范例

2.2同步与死锁

现在通过Runnable接口实现多线程,并产生3给线程对象,同时卖5张票。

public class MyThread implements Runnable
{
    private int ticket=5;
   public void run()
   {
       for (int i = 0; i < 100; i++) {
           if(ticket>0)
           {
               try
               {
                   Thread.sleep(300);
               }catch (InterruptedException e)
               {
                   e.printStackTrace();
               }
               System.out.println("卖票:ticket="+ticket--);
           }
       }
   }
}


public class ThreadDemo01
{
    public static void main(String[] args) {
        MyThread mt=new MyThread();
        Thread thread1=new Thread(mt);
        Thread thread2=new Thread(mt);
        Thread thread3=new Thread(mt);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

从程序运行结果可以发现,程序加入延迟操作,但是在程序最后出现负数的情况,原因是在一个线程还没在对票数相减时,其他线程就已经把票数相减了。

2.3使用同步解决问题

所谓的同步就是多个操作在同一时间段内只能有一个线程进行,其他线程需要等待,解决资源共享的同步操作,可以使用同步代码块和同步两种方法完成。

使用同步代码块解决同步的问题

public class MyThread implements Runnable
{
    private int ticket=5;
   public void run()
   {
       for (int i = 0; i < 100; i++) {
          synchronized (this){
           if(ticket>0)
           {
               try
               {
                   Thread.sleep(300);
               }catch (InterruptedException e)
               {
                   e.printStackTrace();
               }
               System.out.println("卖票:ticket="+ticket--);
           }
          }
       }
   }
}


public class ThreadDemo01
{
    public static void main(String[] args) {
        MyThread mt=new MyThread();
        Thread thread1=new Thread(mt);
        Thread thread2=new Thread(mt);
        Thread thread3=new Thread(mt);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

同步方法

public class MyThread implements Runnable
{
    private int ticket=5;
   public void run()
   {
       for (int i = 0; i < 100; i++) {
           this.sale();
       }
   }
       public synchronized void sale()
   {
       if(ticket>0)
       {
           try
           {
               Thread.sleep(300);
           }catch (InterruptedException e)
           {
               e.printStackTrace();
           }
           System.out.println("卖票:ticket="+ticket--);
       }
   }

}
public class ThreadDemo01
{
    public static void main(String[] args) {
        MyThread mt=new MyThread();
        Thread thread1=new Thread(mt);
        Thread thread2=new Thread(mt);
        Thread thread3=new Thread(mt);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

此代码块完成了之前同步代码块同样的功能

2.4死锁

死锁就是指两个线程都在等待对方先完成,造成了程序的停滞,一般程序死锁都是在程序运行时候出现的。

public class List
{
    public void say()
    {
        System.out.println("李四对张三说:你给书,我就把画给你");
    }
    public void get()
    {
        System.out.println("李四得到书了");
    }
}
public class Zhangsan
{
    public void say()
    {
        System.out.println("张三对李四说:你给画,我就把书给你");
    }
    public void get()
    {
        System.out.println("张三得到画了");
    }
}
public class ThreadDead implements Runnable
{
    private static Zhangsan zs=new Zhangsan();
    private static List is=new List();
    public boolean flag=false;
    public void run() {
        if (flag) {
            synchronized (zs) {
                zs.say();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (is) {
                    zs.get();
                }
            }
        } else {
            synchronized (is) {
                is.say();

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (zs) {
                    is.get();
                }
            }
        }
    }
}

public class Test
{
    public static void main(String[] args) {
        ThreadDead t1 = new ThreadDead();
        ThreadDead t2 = new ThreadDead();
        t1.flag=true;
        t2.flag=false;
        Thread thA=new Thread(t1);
        Thread thB=new Thread(t2);
        thA.start();
        thB.start();
    }
}

从程序可以发现,两个线程都在彼此等待着对方的执行完成,这样,程序无法继续下去执行,从而造成死锁的状态。

2.5线程的生命周期

在java中一个线程对象都有自己的生命周期,如果要控制线程的生命周期,首先要认识生命周期。

对于线程中的suspend()/stop()/resume() 3种方法并不推荐使用,因为可能回产生死锁的问题。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值