【Java并发】基础
发布时间:2020-12-15 07:28:52 所属栏目:Java 来源:网络整理
导读:一、概述 1.1 线程与进程区别 1.2 多线程引发的性能问题 二、多线程创建方式 2.1 第一种-继承Thread类 2.2 第二种-实现Runnable接口 2.3 第三种-实现Callable接口 2.4 常用线程构造函数 2.5 使用继承Thread类还是使用实现Runnable接口好? 三、线程基础知识
一、概述1.1 线程与进程区别
1.2 多线程引发的性能问题
二、多线程创建方式2.1 第一种-继承Thread类
//1. 继承thread类,重写run方法,run方法中,需要线程执行代码 class ThreadDemo01 extends Thread { // run方法中,需要线程需要执行代码 @Override public void run() { for (int i = 0; i < 10; i++) { System.out.print("子线程id:" + this.getId() + ","); System.out.print("子线程name:" + getName()+","); System.out.println("子线程--->i:" + i); } } } // 1.什么是线程 线程是一条执行路径,每个线程都互不影响。 // 2.什么是多线程,多线程在一个进程中,有多条不同的执行路径,并行执行。目的为了提高程序效率。 // 3.在一个进程中,一定会主线程。 // 4.如果连线程主线程都没有,怎么执行程序。 // 线程几种分类 1. 用户线程、守护线程 // 2. 主线程 子线程 GC线程 public class T001_CreateWithThread { // 交替执行 public static void main(String[] args) { System.out.println("main... 主线程开始..."); // 1.创建线程 ThreadDemo01 threadDemo01 = new ThreadDemo01(); // 2.启动线程 threadDemo01.start(); for (int i = 0; i < 10; i++) { System.out.println("main---> i: " + i); } System.out.println("main... 主线程结束..."); } }
System.out.println("-----多线程创建开始-----"); new Thread() { public void run() { for (int i = 0; i < 10; i++) { System.out.println("线程 -- " + Thread.currentThread().getName() + "-->" + i); } }; }.start(); System.out.println("-----多线程创建结束-----"); 2.2 第二种-实现Runnable接口
class ThreadDemo02 implements Runnable { public void run() { for (int i = 0; i < 10; i++) { System.out.println(" 子 i:" + i); } } } // 1.实现runable接口,重写run方法 public class T002_CreateWithRunnable { public static void main(String[] args) { System.out.println("main... 主线程开始..."); // 创建线程 ThreadDemo02 threadDemo02 = new ThreadDemo02(); /* * 这里 用了Thread的另一个构造方法, * 该构造方法可以传入一个Runnable的实现类 * 而我们查看Thread的源码可以得知,Thread类 原本就实现了Runnable * 所里也可以传入一个Thread的对象,这样就可以把一个Thread对象中的run() * 方法交由其他的线程进行调用 */ Thread t1= new Thread(threadDemo02); // 启动线程 t1.start(); for (int i = 0; i <10; i++) { System.out.println("main..i:"+i); } System.out.println("main... 主线程结束..."); } }
System.out.println("-----多线程创建开始-----"); Thread thread = new Thread(new Runnable() { public void run() { for (int i = 0; i< 10; i++) { System.out.println("i:" + i); } } }).start(); System.out.println("-----多线程创建结束-----"); 2.3 第三种-实现Callable接口/** * * Callbale接口 可以又返回值,可以抛出异常, * 而Runnable接口 中的run方法没有返回值,异常只能捕获 * * @author hao * */ public class T002_CreateWithCallable { public static void main(String[] args) throws InterruptedException,ExecutionException { MyCallable mc = new MyCallable(); FutureTask<Integer> ft = new FutureTask<Integer>(mc); Thread thread = new Thread(ft); thread.start(); System.out.println(ft.get()); } } class MyCallable implements Callable<Integer> { public Integer call() throws Exception { return 124; } } 2.4 常用线程构造函数
2.5 使用继承Thread类还是使用实现Runnable接口好?
三、线程基础知识3.1 常用线程API概述
3.2 守护线程
/* * 什么是守护线程? 守护线程 进程线程(主线程挂了) 守护线程也会被自动销毁. * 该示例中我们手动将子线程设置为守护线程, * 当其他线程(该例中只有主线程)停止时守护线程也会终止 */ public class DaemonThread { public static void main(String[] args) { Thread thread = new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(100); } catch (Exception e) { // TODO: handle exception } System.out.println("我是子线程..."); } } }); thread.setDaemon(true); thread.start(); for (int i = 0; i < 10; i++) { try { Thread.sleep(100); } catch (Exception e) { } System.out.println("我是主线程"); } System.out.println("主线程执行完毕!"); } }
3.3 yield方法
/** * yield方法的作用是放弃当前的CPU资源, * 将它让给其他的任务去占用CPU执行时间, * 但是放弃的时间不确定,有可能刚刚放弃,马上又获得CPU时间片 * @author hao * */ public class T009_Yield { public static void main(String[] args) { YeildTestThread t1 = new YeildTestThread(); t1.start(); } } class YeildTestThread extends Thread{ @Override public void run() { super.run(); long beginTime = System.currentTimeMillis(); int count =0; for(int i =0;i<5000000;i++) { //Thread.yield(); count = count +(i+1); } long endTime = System.currentTimeMillis(); System.out.println("用时:"+(endTime-beginTime)+"毫秒!"); } }
3.4 join()方法作用
//创建一个线程,子线程执行完毕后,主线程才能执行。 public class T010_Join { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(new Runnable() { public void run() { for (int i = 0; i < 5; i++) { System.out.println("子线程,i:" + i); } } }); t1.start(); // 当前线程释放资格权,等t1执行完毕之后,才会继续进行执行。 t1.join(); for (int i = 0; i < 5; i++) { System.out.println("main线程,i:" + i); } } }
public class JoinThreadDemo02 { public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { public void run() { for (int i = 0; i < 20; i++) { System.out.println("t1,i:" + i); } } }); Thread t2 = new Thread(new Runnable() { public void run() { try { t1.join(); } catch (Exception e) { } for (int i = 0; i < 20; i++) { System.out.println("t2,i:" + i); } } }); Thread t3 = new Thread(new Runnable() { public void run() { try { t2.join(); } catch (Exception e) { } for (int i = 0; i < 20; i++) { System.out.println("t3,i:" + i); } } }); t1.start(); t2.start(); t3.start(); } } 3.5 优先级
public class T011_Priority { public static void main(String[] args) { PrioritytThread prioritytThread = new PrioritytThread(); Thread t1 = new Thread(prioritytThread); Thread t2 = new Thread(prioritytThread); t1.start(); // 注意设置了优先级, 不代表每次都一定会被执行。 只是CPU调度会有限分配 t1.setPriority(10); t2.start(); } } class PrioritytThread implements Runnable { public void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().toString() + "---i:" + i); } } } 四、停止线程
4.1 interrupt方法介绍调用interrupt()方法后线程并没有马上停止,仅仅是在当前线程中打了一个停止的标记,并不是真正的停止线程。 4.2 判断线程是否是停止状态
public static boolean interrupted() { return currentThread().isInterrupted(true); } public boolean isInterrupted() { return isInterrupted(false); } /** * Tests if some Thread has been interrupted. * The interrupted state is reset or not * based on the value of ClearInterrupted that is passed. * * 判断某些线程是否已经中断。 * 根据传入的ClearInterrupted值来决定是否要重置中断的状态 */ private native boolean isInterrupted(boolean ClearInterrupted); 4.3 利用异常的方式停止线程/** * 利用抛出异常的方式来终止线程 * @author hao * */ public class Test_ExceptionInterrupt { public static void main(String[] args) { try { ExcepThread t = new ExcepThread(); t.start(); Thread.sleep(2000); t.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("end!"); } } class ExcepThread extends Thread{ @Override public void run() { this.stop(); try { for(int i=0;i<500000;i++) { if(Thread.interrupted()) { System.out.println("已经是停止状态了,我要退出了!"); // break; throw new InterruptedException(); } System.out.println("i "+(i+1)); } System.out.println("我被输出了 。线程并未停止! 只是for循环被中断了"); } catch (InterruptedException e) { System.out.println("catch t "); e.printStackTrace(); } } } 五、多线程运行状态5.1 线程状态概览
5.2 新建状态(NEW)
5.3 可运行状态(Runnbale)
5.4 阻塞状态(Blocked)
5.5 无限期等待(Waiting)
5.6 限期等待(Timed Waiting)
5.7 死亡(Terminated)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |