JAVA Class22

学习内容:

进程与线程:进程是一个单独运行的程序,有自己的资源与内存,线程是在进程内部同时做的事情,一个进程可以有一个或多个线程,而一个线程只能有一个进程。

多线程:多线程简单来说就是在同一时间执行多个方法。

一般情况下,在main方法内调用的方法会依次执行,一次只能执行一个,因为只有一个main线程(主线程)。

1.多线程有三种实现方式:

(1)继承Thread类,重写run方法,注意,这里重写的run方法时Runnable接口提供的抽象方法,因为Thread类也实现了Runnable接口

public class Test extends Thread{
    @Override
    public void run() {
        for(int i=0;i<5;i++) {
            System.out.println("线程正在运行"+i);
        }
    }
    public static void main(String[] args) {
        Test t = new Test();
        t.start();
        for(int i=0;i<5;i++) {
            System.out.println("main线程正在执行"+i);
        }
    }

}

(2)实现Runnable接口,重写run方法

这种方式耦合度更低:

public class Test implements Runnable{//耦合度更低,开启线程与run方法调用分离
    @Override
    public void run() {
        for(int i=0;i<10;i++) {
            System.out.println(Thread.currentThread().getName()+"正在执行"+i);
        }
    }
    public static void main(String[] args) {
        Test t = new Test();
        new Thread(t).start();//将实现runnable接口的类对象传入,最终调用的是t的run方法
        for(int i=0;i<10;i++) {
            System.out.println(Thread.currentThread().getName()+"正在执行"+i);
        }
    }

}

(3)匿名类

public class Test {
    public static void function() {
        for(int i=0;i<1000;i++) {
            System.out.println(i);
        }
    }
    public static void main(String[] args) {
        Thread t1 = new Thread() {
            public void run() {
                function();
            }
        };
        t1.start();
        Thread t2 = new Thread() {
            public void run() {
                for(int j=0;j<100;j++) {
                    System.out.println(j+"s");
                }
            }
        };
        t2.start();
        new Thread(new Runnable() {
                public void run() {
            
                }    
            })
        {}.start();
    }

}

区别:

继承Thread类:是通过该类被实例化的对象去调用start方法,利用start方法调用被重写的run方法,本质上是利用Thread类的子类调用了run方法。
继承Runnable接口:是实例化一个Runnable的实现类,然后通过实例化一个Thread对象,先调用Thread类的start方法,然后通过调用Thread类的run方法,间接调用Runnable实现类的run方法,真正的业务方法和调用类之间做了分离,耦合度更低。

2.常用方法:

currentThread() 获取当前线程,返回Thread

getName() 获取线程名

sleep(毫秒值) 线程休眠一段时间,时间到后继续执行,必须进行try catch

wait() 线程等待,直到被notify

notify() 唤醒等待线程

notifyAll() 唤醒所有等待线程

利用sleep()做一个简单的时钟:

import java.text.SimpleDateFormat;
import java.util.Date;

public class Clock implements Runnable{
    @Override
    public void run() {
        while(true) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            try {
                Thread.sleep(1000);
                String clock = sdf.format(new Date(System.currentTimeMillis()));
                System.out.println(clock);
            } catch (Exception e) {
                e.printStackTrace();
            }    
        }
    }
}

public class TestClock {

    public static void main(String[] args) {
        Clock c = new Clock();
        new Thread(c).start();
    }

}

 3.线程池

线程池的原理:创造一个容器,添加多个线程,使用线程时取出一个线程,使用完毕后再添加回线程池。

线程工厂类:Executors(JDK1.5)

public class ThreadPool {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(2);//新建一个线程池,容量为2//销毁线程池
        service.shutdown();
   }
}

使用方式:

(1)实现Runable接口,重写run方法

public class Run implements Runnable{
    @Override
    public void run() {
        for(int i=0;i<5;i++) {
            System.out.println(Thread.currentThread().getName()+"正在运行:"+i);
        }
    }
}

public class ThreadPool {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(2);//新建一个线程池
        Run r = new Run();
        service.submit(r);//添加线程对象
        service.submit(r);
   }
}

(2)实现Callable接口,重写call方法,注意,Callable接口有泛型,call方法有返回值

public class Call implements Callable<String>{
    @Override
    public String call() throws Exception {
        return "abc";
    }
}

public class ThreadPool {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(2);
        Call c = new Call();
        Future<String> f = service.submit(c);//接收返回值
        try {
            System.out.println(f.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 4.线程状态以及状态间的关系

线程分为五种状态:

  • 新建状态:

    使用 new 关键字和 Thread 类或其子类建立一个线程对象后,该线程对象就处于新建状态。它保持这个状态直到程序 start() 这个线程。

  • 就绪状态:

    当线程对象调用了start()方法之后,该线程就进入就绪状态。就绪状态的线程处于就绪队列中,要等待JVM里线程调度器的调度。

  • 运行状态:

    如果就绪状态的线程获取 CPU 资源,就可以执行 run(),此时线程便处于运行状态。处于运行状态的线程最为复杂,它可以变为阻塞状态、就绪状态和死亡状态。

  • 阻塞状态:

    如果一个线程执行了sleep(睡眠)、suspend(挂起)等方法,失去所占用资源之后,该线程就从运行状态进入阻塞状态。在睡眠时间已到或获得设备资源后可以重新进入就绪状态。可以分为三种:

    • 等待阻塞:运行状态中的线程执行 wait() 方法,使线程进入到等待阻塞状态。

    • 同步阻塞:线程在获取 synchronized 同步锁失败(因为同步锁被其他线程占用)。

    • 其他阻塞:通过调用线程的 sleep() 或 join() 发出了 I/O 请求时,线程就会进入到阻塞状态。当sleep() 状态超时,join() 等待线程终止或超时,或者 I/O 处理完毕,线程重新转入就绪状态。

  • 死亡状态:

    一个运行状态的线程完成任务或者其他终止条件发生时,该线程就切换到终止状态。

原文地址:https://www.cnblogs.com/whwjava/p/8928850.html