Java多线程详解,process 线程

  Java多线程详解,process 线程

  00-1010进程与线程的关系操作系统如何管理进程的并行性和并发性?创建线程的方法?串行执行和并发执行?线程中的一个重要方法?中断线程并等待线程休眠。

  00-1010运行在操作系统中的程序是一个进程,比如QQ、玩家、游戏等……程序是指令和数据的有序集合,没有运行意义,是一个静态的概念。

  进程和线程都是为了处理并发编程场景而设计的,但是进程存在问题,在进程频繁断开的情况下,创建和释放资源的效率较低。相比之下,线程的重量更轻,创建和释放资源的效率更高。

  过程是独立的。每个进程都有自己独立的虚拟地址空间。如果一个进程挂起,不会影响其他进程。同一进程中的多个线程共享同一个内存空间。如果一个线程挂起,可能会影响其他线程,甚至导致整个进程崩溃…

  进程是执行程序的执行进程。它是一个动态的概念,是系统资源分配的单位。

  通常,一个进程可以包含多个线程(如果你把一个进程想象成一个工厂,那么线程就是工厂中的生产线,一个工厂内部可以有一条生产线,也可以有多条生产线)。当然,一个进程中至少要有一个线程,否则,没有任何意义。线程是CPU调度和执行的单位。

  真正的多线程是指拥有多个CPU,也就是多核。

  它是线程独立执行的路径。

  程序执行时,后台会有多个线程,比如主线程和GC线程,即使没有自己创建的线程。

  每个线程都在自己的工作内存中交互,内存操作不当会造成数据不一致。

  00-1010 1.先描述一个过程。(显式排除流程上方的一些相关属性)

  2.组织几个流程。(有些数据结构是用来把大量描述过程的信息放在一起,方便增删查改。典型的实现是通过使用双向链表来串每个进程的PCB)。

  00-1010并行3360微观上,两个CPU核,同步你的是执行两个任务的代码。

  并发3360微观上,一个CPU核,先执行任务1,再执行任务2,再执行任务3,只要切换够快,宏观上看起来好像同时执行了很多任务。

  并发和并行只能在微观层次上区分,而不能在宏观层次上区分。这里所有的区别都是运行西欧系统自动调度的结果。正因为并行和并发对于查看来说是无法区分的,所以我们在写代码的时候也不区分,只是在研究操作系统进程的调度的时候稍微区分一下。并发基本上是作为其他场景的通称。

  00-1010方法1 3360通过Thread类创建线程,这是最简单的方法。创建一个子类,继承Thread类,重写run方法。

  包装线;类MyThread扩展Thread { @ Override public void run(){ system . out . println( hello Thread );} } public class demo 1 { public static void main(String[]args){ Thread t=new MyThread();t . start();}}方法2 :

  导入Java . util . scanner;类MyThread2扩展了Thread { @ Override public void run(){ system . out . println( hello Thread!);试试{ thread . sleep(1000);} catch(interrupted exception e){ e . printstacktrace();} } } public class demo 2 { public static void main 1(String[]args){ Thread t=new myth read 2();t . start();while(true){ system . out . println( hello main!);试试{ thread . sleep(1000);} catch(interrupted exception e){ e . printstacktrace();}}}方法:创建匿名内部类,继承Thread类,重写run方法,更新匿名内部类的实例。

  惩罚学生的工作

  rt java.util.*;class MyRunnable implements Runnable { @Override public void run() { System.out.println("hello!"); }}public class Demo3 { public static void main1(String[] args) { Thread t = new Thread(new MyRunnable()); t.start(); }方法四: 同样使用了匿名内部类.

  

public class Demo4 { public static void main(String[] args) { Thread t = new Thread(){ @Override public void run() { System.out.println("hello thread"); } }; t.start(); }}

方法五: 使用了lambda表达式.

 

  

import java.util.*;public class Demo5 { public static void main1(String[] args) { Thread t = new Thread(new Runnable() { @Override public void run() { System.out.println("hello thread"); } }); t.start(); }

 

  

串行执行和并发执行

public class Demo7 { private static final long count = 10_0000_0000; public static void serial(){ //吉利程序执行时间 long beg = System.currentTimeMillis(); long a = 0; for (int i = 0; i < count; i++) { a++; } long b = 0; for (int i = 0; i < count; i++) { b++; } long end = System.currentTimeMillis(); System.out.println("消耗时间: "+ (end - beg) + "ms"); } public static void concurrency() throws InterruptedException { long beg = System.currentTimeMillis(); Thread t1 = new Thread(()->{ long a = 0; for (int i = 0; i < count; i++) { a++; } }); t1.start(); Thread t2 = new Thread(()->{ long b = 0; for (int i = 0; i < count; i++) { b++; } }); t2.start(); //此处布恩那个直接记录结束时间,别忘了,现在这个时间戳的代码是在main 线程中 //main t1 t2 是并发执行关系,此处t1 t2 还没执行完呢,这里就开始记录结束时间了,这显然是不准确的. //正确做法应该是让main线程等地啊t1 t2跑完了,在记录结束时间 //join 效果就是等待线程结束,t1.join 就是让main 线程等待 t1 结束,t2.join让main 线程等待t2结束 t1.join(); t2.join(); long end = System.currentTimeMillis(); System.out.println("消耗时间: "+ (end - beg)+ "ms"); } public static void main(String[] args)throws InterruptedException { // serial(); concurrency(); }}

 

  

Thread中的一次额重要方法

1.start

 

  决定了系统中是不是真的创建出线程,run单纯的是一个普通的方法,描述了任务的内容,start则是一个特殊的方法,内部会在系统中创建线程.

  

 

  

public class Demo9 { public static void main(String[] args) { Thread t = new Thread(()->{ while (true) { System.out.println("hello thread1"); try { Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); } } }); t.start(); //t.run(); while (true) { System.out.println("hello main"); try { Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); } } }}

run方法只是一个普通的方法,在main线程里面调用run,其实并没有创建线程,这个循环仍是在main中执行的.

 

  

 

  

 

  

中断线程

中断线程:让一个线程停下来.

 

  线程停下来的关键,是要让对应的run方法执行完.

  (1).可以手动设置一个标志位,来控制线程是否要执行结束.

  

public class Demo10 { private static boolean isQuit = false; public static void main(String[] args) { Thread t = new Thread(()->{ while (!isQuit) { System.out.println("hello thread"); try { Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); } } }); t.start(); //只要把这个isQuit 设为true ,此时这个循环就退出了,进一步run 就执行完了,再进一步就是线程结束了 try{ Thread.sleep(5000); }catch (InterruptedException e){ e.printStackTrace(); } isQuit = true; System.out.println("终止t线程"); }}

(2).使用thread中内置的一个标志位来进行判定.

 

  

public class Demo11 { public static void main(String[] args) { Thread t = new Thread(()->{ while(!Thread.currentThread().isInterrupted()){ System.out.println("hello thread"); try { Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); //当触发异常之后,立即就退出循环 break; } } }); t.start(); try{ Thread.sleep(5000); }catch (InterruptedException e){ e.printStackTrace(); } //在线程中 调用interrupt 方法 来终端这个线程 //t.interrupt 的意思是让t线程被中断!! t.interrupt(); }}

 

  

线程等待

多个线程之间,调度顺序是不确定的,线程之间的执行是按照调度器来安排的,这个过程可以视为无序或者随机的,有些时候,我们需要控制线程的顺序,线程等待就是其中一种手段,此处的线程等,主要控制线程结束的先后顺.

 

  join: 调用join的时候,那个线程调用的join,那个线程就会阻塞等待,等到对应的线程执行完毕为止(对应线程的run执行完).

  

public class Demo12 { public static void main(String[] args) { Thread t = new Thread(()->{ for (int i = 0; i < 5; i++) { System.out.println("hello thread"); try{ Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); } } }); t.start(); //在主线程中可以使用一个等待操作,来等待t线程执行结束 try{ t.join(10000); }catch (InterruptedException e){ e.printStackTrace(); } }}

 

  

线程休眠(sleep)

 

  如果线程调用了sleep方法,这个PCB就会进入到阻塞队列.

  当睡眠时间到了,系统就会把这个PCB从阻塞队列挪回到就绪队列.

  实例:

  

public class Demo14 { public static void main(String[] args) throws InterruptedException { Thread t = new Thread(()->{ while (true) { //z这里啥都不能有 try{ Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); } } }); t.start(); Thread.sleep(1000); System.out.println(t.getState()); }}

到此这篇关于Java超详细讲解多线程中的Process与Thread的文章就介绍到这了,更多相关JavaProcess与Thread内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: