### 多线程使用多线程可以让程序充分利用CPU资源,提高CPU的使用效率,从而解决高并发带来的负载均衡问题。优点:- CPU资源得到更合理的利用。- 程序设计更加简洁。- 程序响应更快,运行效率更高。缺点:- 需要更多的内存空间来支持多线程。- 多线程并行访问的情况可能会影响数据的准确性。- 数据被多线程共享,可能出现死锁的情况。### 进程是指计算机在某个数据集合上进行的一次运行活动,是系统进行资源分配调度的基本单位。简单理解,进程就是计算机正在运行的一个具体的应用程序。线程是进程的基本单位,程序执行的最小单元,为独立完成程序中的某个功能而存在,一个进程是由一个或者多个线程组成的。### 进程和线程的区别进程在运行时拥有独立的存储空间,每个进程所占用的内存都是独立的,互不干扰。多个线程是共享内存空间的,每个线程都有其执行的入口和出口,但是线程不能独立执行,必须依赖于进程,由进程提供多线程的执行控制。多线程:在一个进程中,多个线程同时执行。### Java中线程的使用- 继承Thread类 - 创建自定义类并继承Thread - 重写Thread的run方法,并编写该线程的业务逻辑代码```javapackage thread;public class MyThread extends Thread { @Override public void run() { // TODO Auto-generated method stub for(int i = 0; i < 1000;i++) { System.out.println("-------------MyThread-----------"); } }}``````javapackage thread;public class Test { public static void main(String[] args) { for(int i = 0; i < 1000;i++) { System.out.println("-----------------Test"); } MyThread myThread = new MyThread(); myThread.start(); }}```- 实现Runnable接口 - 创建自定义类并实现Runnable接口 - 实现run方法,编写该线程的业务逻辑代码```javapackage thread;public class MyRunnable implements Runnable { @Override public void run() { // TODO Auto-generated method stub for(int i = 0; i < 1000;i++) { System.out.println("!===Runnable===!"); } }}``````javapackage thread;public class Test2 { public static void main(String[] args) { MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); thread.start(); MyThread myThread = new MyThread(); myThread.start(); }}```### 线程的状态线程共有5种状态,在特定的情况下,线程可以在不同的状态之间完成切换,5种状态如下所示。- 创建状态:实例化一个新的线程对象,没有启动。```javaMyThread myThread = new MyThread();MyRunnable myRunnable = new MyRunnable();Thread thread = new Thread(myRunnable);```- 就绪状态:线程对象创建好之后,调用该对象的start方法启动线程,该状态下的线程对象位于可运行线程池中,等待系统为其分配CPU资源。```javamyThread.start();thread.start();```- 运行状态:就绪状态的线程在某个时间段内获取到了CPU资源,从而执行程序,完成它的任务。- 阻塞状态:运行状态的线程因某些原因暂时放弃CPU资源,停止执行程序,并在解除阻塞之后也不能直接回到运行状态,而是重新回到就绪状态,等待获取CPU资源。- 终止状态:线程执行完毕或者因为异常退出,该线程的生命周期结束。
Test.java
package demo;import java.util.Scanner;public class Test { private static Scanner scanner; private static Sedan sedan; private static Bus bus; private static Truck truck; private static Car[] cars; static { scanner = new Scanner(System.in); sedan = new Sedan("小轿车","黑色"); bus = new Bus("大巴车","绿色"); truck = new Truck("卡车","蓝色",2); cars = new Car[3]; cars[0] = sedan; cars[1] = bus; cars[2] = truck; } public void showCars() { System.out.println("车辆名称\t\t车辆颜色\t\t座位数\t\t载重量"); Car car = null; for(int i = 0; i < cars.length;i++) { car = cars[i]; if(car instanceof Sedan) { Sedan sedan = (Sedan)car; System.out.println(sedan.getName()+"\t\t"+sedan.getColor()+"\t\t"+sedan.seatNum()); } if(car instanceof Bus) { Bus bus = (Bus)car; System.out.println(bus.getName()+"\t\t"+bus.getColor()+"\t\t"+bus.seatNum()); } if(car instanceof Truck) { Truck truck = (Truck)car; System.out.println(truck.getName()+"\t\t"+truck.getColor()+"\t\t"+truck.seatNum()+"\t\t"+truck.getweight()+"吨"); } } System.out.println("1.小轿车\t2.大巴车\t3.卡车"); System.out.print("请选择要修改的车辆:"); int num = scanner.nextInt(); switch(num) { case 1: update("sedan"); break; case 2: update("bus"); break; case 3: update("truck"); break; } } public void update(String type) { String name = null; String color = null; System.out.print("请输入车辆名称:"); name = scanner.next(); System.out.print("请输入车辆颜色:"); color = scanner.next(); //小轿车 if(type.equals("sedan")) { Sedan sedan = new Sedan(name,color); cars[0] = sedan; } if(type.equals("bus")) { Bus bus = new Bus(name,color); cars[1] = bus; } if(type.equals("truck")) { System.out.print("请输入载重量:"); int weight = scanner.nextInt(); if(weight > 12) { try { throw new CarException("卡车的载重量不能超过12吨!"); } catch (CarException e) { // TODO Auto-generated catch block e.printStackTrace(); return; } } Truck truck = new Truck(name,color,weight); cars[2] = truck; } showCars(); } public static void main(String[] args) { Test test = new Test(); test.showCars(); }}
MyTest.java
public class MyTest { public void test() { for(int i = 0; i < 10; i++) { System.out.println("--------------MyTest"); } }}
MyThread.java
public class MyThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub for(int i = 0; i < 1000; i++) { System.out.println("--------------MyTest"); } }}
Test.java
public class Test { public static void main(String[] args) {// MyThread myThread = new MyThread();// myThread.start(); MyTest mytest = new MyTest(); mytest.test(); for(int i = 0; i < 1000; i++) { System.out.println("++++++++++++++Test"); }// MyTest mytest = new MyTest();// mytest.test(); }}
MyRunnable.java
package thread;public class MyRunnable implements Runnable { @Override public void run() { // TODO Auto-generated method stub for(int i = 0; i < 1000;i++) { System.out.println("!===Runnable===!"); } }}
MyThread.java
package thread;public class MyThread extends Thread { @Override public void run() { // TODO Auto-generated method stub for(int i = 0; i < 1000;i++) { System.out.println("-------------MyThread-----------"); } }}
Test.java
package thread;public class Test { public static void main(String[] args) { for(int i = 0; i < 1000;i++) { System.out.println("-----------------Test"); } MyThread myThread = new MyThread(); myThread.start(); }}
Test2.java
package thread;public class Test2 { public static void main(String[] args) { MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); thread.start(); try { thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("休眠结束"); }}