当前位置: 首页 > news >正文

初识多线程

多线程的出现

  • 在过去使用网络编程时,编写的服务器使用 多进程(CGI) 来回复网页的请求。而每一个进程的使用需要申请资源、释放资源(CPU、内存、硬盘等)。请求往往是接连不断的,一连接一个的请求就会导致频繁的创建、销毁进程,对操作系统的开销是比较大的。为了解决频繁创建、销毁的问题,多线程在进程的基础上进行了改进。

线程的工作方式

多线程工作

  • 进程的创建申请的资源由线程共享,线程的调度基本与进程相同(资源不仅限于内存)
  • 线程的PCB中也有 状态、优先级、记账信息、上下文等属性
  • 一个进程可以有多个PCB(拥有多个线程)
  • 线程之间可能会互相干扰,引起线程安全问题
  • 第一个线程的开销资源程度是最大的,申请的资源不足时,新的线程创建也可能会伴随开销资源,不是一次性买卖。
  • 线程不是越多越好,线程太多会导致调度占用过多。

多线程编程

1) 继承Thread

  • 创建一个MyThread类继承Thread类,重写run方法作为此类的入口。在主类中实例化MyThread对象,通过对象t调用start方法开启多线程,将会死循环打印"HELLO THREAD"和"hello world",线程的随机调度为抢占式执行,不分先后。
class MyThread extends Thread{    //创建一个类继承Thread类@Overridepublic void run() {  //重写父类Thread的run方法while (true){System.out.println("HELLO THREAD");      }}
}
public class ThreadDemo1 {      //主类public static void main(String[] args) {  //主线程入口Thread t = new MyThread();    //创建MyThread类的对象t.start();         //MyThread类中run方法的入口,执行这条指令后run方法会开始执行,并且主类当中也会继续往下执行,二者不分先后while (true){System.out.println("hello world");}}
}
  • 为了将结果可视化,通过Thread.sleep(Thread类里面的静态方法)将线程停顿1000ms。jvm会自动识别sleep方法将当前线程sleep,不会发生此线程干扰其他线程sleep情况。在每一行的打印之后加上Thread.sleep(1000);出现异常之后try-ctach捕获异常
class MyThread2 extends Thread{    //创建一个类继承Thread类@Overridepublic void run() {  //重写父类Thread的run方法while (true){System.out.println("HELLO THREAD");try {Thread.sleep(1000);  //停顿一秒} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
public class ThreadDemo2 {      //主类public static void main(String[] args) {  //主线程入口Thread t = new MyThread2();    //创建MyThread类的对象t.start();         //MyThread类中run方法的入口,执行这条指令后run方法会开始执行,并且主类当中也会继续往下执行,二者不分先后while (true){System.out.println("hello world");try {Thread.sleep(1000); //停顿一秒} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
打印结果像这样,每秒一组打印
HELLO THREAD
HELLO THREAD
-------------------
hello world
HELLO THREAD
-------------------
hello world
HELLO THREAD
-------------------
hello world
hello world

2) 接上接口Runnable

  • Thread类本身实现了Runnable接口
  • 继承Thread既将线程和任务绑定在一起。如果一个任务需要被多个线程使用,用Runnable接口进行解耦合将会更方便,多个线程可以共享同一个Runnable实例。
class MyThread3 implements Runnable {  //创建一个类接上接口'Runnable'@Overridepublic void run() {  //重写父类Thread的run方法while (true) {System.out.println("hello runnable");try {Thread.sleep(1000);    //停顿1秒} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}public class ThreadDemo3 {public static void main(String[] args) {  //主线程Runnable runnable = new MtThread3(); //实例化runnable对象Thread t = new Thread(runnable);  //将runnable对象传给Thread类t.start();   //进入MyThread3的run方法while (true) {System.out.println("hello main");try {Thread.sleep(1000);  //停顿一秒} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

3) 使用匿名内部类继承Thread

  • 匿名内部类可以重写外部类的方法,并且不需要创建内部类的名字
public class ThreadDemo4 {public static void main(String[] args) {Thread t = new Thread() { //匿名内部类重写了run方法@Overridepublic void run() {  //重写父类Thread的run方法while (true) {System.out.println("hello thread");try {Thread.sleep(1000); //停顿一秒} catch (InterruptedException e) {throw new RuntimeException(e);}}}}; t.start(); //进入内部类run方法while (true) {System.out.println("hello main");try {Thread.sleep(1000); //停顿一秒} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

4) 使用匿名内部类接上接口Runnable

public class ThreadDemo5 {public static void main(String[] args) {Thread t = new Thread(new Runnable() { //传入Runnable实例//匿名内部类重写了run方法@Overridepublic void run() {while (true) {System.out.println("hello runnable");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}});    //注意内部类括号与分号t.start();while (true) {System.out.println("hello main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

5) lambda表达式(最简便的方法)

  • lambda表达式可以实例化一个只有一个抽象方法的接口
public class ThreadDemo6 {public static void main(String[] args) {Thread t = new Thread(() -> {    //这里实际上就已经在重写run方法了while (true) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t.start();while (true) {System.out.println("hello main");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}
http://www.dtcms.com/a/538220.html

相关文章:

  • 甘肃电子商务网站建设南京百度网站建设
  • 自动驾驶中的传感器技术72——Navigation(9)
  • 津南网站建设展位设计
  • 运城网站建设北京网站设计研究与开发公司
  • 专做polo衫的网站wordpress中文个人博客主题
  • 自己做网赌网站dedecms做网站视频
  • FFmpeg 基本数据结构 URLContext分析
  • cpanel 子网站网站开发属于哪一类
  • 档案网站建设经验成都网站建设索q479185700
  • 安陆网站建设杭州关键词优化外包
  • 网站开发语言选择温州 网站建设
  • 做网站 用什么做数据库最好房产网名字叫啥好听
  • 网站开发项目架构河北网站制作 网站开发
  • 禁止国内ip访问 网站wordpress换logo
  • 智能体最佳实践的方法论(三):集成
  • 百度 搜索到手机网站网站编程培训公司
  • 如何申请小程序seo优化工具使用教程
  • 网站建设优化两千字四川省城乡住房与建设厅网站
  • 从快手评论数据中挖掘舆情:Python爬虫与文本分析实战
  • Linux 服务管理
  • 软件推荐网站广州市城乡建设网站
  • 应价交易系统网站开发培训方案
  • 面向具身人工智能的二维世界模型综合综述
  • 高端酒店网站模板免费下载怎么简单页网站
  • 网站内容与功能设计手机端网站seo
  • 百度网站没收录公司如何做网站做推广
  • 泉州网站设计招聘网网站建设开票名称
  • 杭州网站建设费用价格青岛无间设计公司网站
  • 手机app制作网站合肥网页设计就业
  • 做网站移交资料企业电话认证