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

进网站备案时间市场调研报告1000字

进网站备案时间,市场调研报告1000字,网站没内容 可以备案么,做网站市场价格多少钱目录 编写第一个多线程程序 1. 方式一 : 继承Thread类, 重写run方法 2. 方式二: 实现Runnable接口, 重写run方法 3. 方式三: 使用Lambda表达式 [匿名内部类] [Lambda表达式] 在上个文章中, 我们了解了进程和线程的相关概念. 那么, 在Java中, 我们如何进行多线程编程呢? …

目录

编写第一个多线程程序

1. 方式一 : 继承Thread类, 重写run方法

2. 方式二: 实现Runnable接口, 重写run方法

3. 方式三: 使用Lambda表达式

[匿名内部类]

[Lambda表达式]


 

在上个文章中, 我们了解了进程和线程的相关概念. 那么, 在Java中, 我们如何进行多线程编程呢?

线程本身是操作系统的一个概念, 操作系统提供一个线程的API, 供程序员调用. 不同的操作系统线程的API是不同的 (Windows的线程API和Linux的线程API差异很大).  但是JVM将不同的系统线程的API都封装好了, 所以我门在不同系统上进行Java的多线程编程时, 不需要关注系统原生的API, 只需要掌握Java这一套API就可以了.

在Java中, Thread类就负责完成多线程的相关开发.

编写第一个多线程程序

我们有多种方式来进行多线程编程.

1. 方式一 : 继承Thread类, 重写run方法

(1) 步骤一: 创建一个线程类

[注]: 这里不需要对Thread进行导包操作, 因为Thread是java.lang包中的类, 默认已经导入.

上述代码, 就创建了一个线程类(MyThread类) 继承自Thread类. 类里重写了run()方法. (run()方法是Runnable接口中定义的一个方法, 用来指定线程要执行的任务, Thread类实现了run方法, 我们定义的MyThread类继承自Thread, 自然需要重写run方法, 来制定我们当前这个线程需要完成什么任务)

(2) 步骤二: 启动线程 

 观察运行结果, 我们可以看到, 现在控制台上正在循环打印"hello thread", 代表我们创建的MyThread线程启动了. 这里涉及到一个方法start(), 这个方法的作用就是启动线程.

[注]: 通过上述代码, 我们看到, main方法中并没有调用run()方法, 但是程序确实执行力main方法里面的内容. 像这种, 我们手动定义但没有手动调用, 被系统自动调用执行的方法, 叫做"回调函数"(Callback Function) 

接下来我们整体看这个代码:

class MyThread extends Thread {@Overridepublic void run() {// 这里写的代码, 就是即将创建出的线程, 要执行的逻辑.while (true) {System.out.println("hello thread");// 该线程需要完成的任务: 循环打印"hello thread".}}
}public class Demo1 {public static void main(String[] args) {MyThread t = new MyThread();t.start(); // 这一步就创建了一个线程}
}

上述代码实际上就是一个进程. 因为调用了main方法, 所以该进程中还有一个执行main方法的线程, 就叫做"主线程". 我们之前说过, 一个进程至少包含一个线程, 这个线程就是主线程.

主线程和t线程会并发/并行地在CPU上调度执行. 宏观表现就是交替打印主线程和t线程中的任务.

class MyThread extends Thread { //创建一个线程类@Overridepublic void run() {// 这里写的代码, 就是即将创建出的线程, 要执行的逻辑.while (true) {System.out.println("hello thread");// 该线程需要完成的任务: 循环打印"hello thread".}}
}public class Demo1 {public static void main(String[] args) {MyThread t = new MyThread(); //实例化一个新线程t.start(); //启动这个线程while (true) {System.out.println("hello main");// 主线程要完成的任务: 循环打印"hello main".}}
}

  

从运行结果来看, 主线程和t线程的任务交替打印. 那么多个线程之间, 谁先去CPU上调度执行, 谁后去CPU上调度执行, 这个顺序是不确定的, 取决于操作系统的内核. 并且, 我们把这种执行方式叫做"抢占式执行".

 * 使用匿名内部类来实现方式一

上述红框框住的代码, 就使用了匿名内部类. 这部分代码完成了3件事: (1) 定义了一个匿名内部类, 这个类一定是Thread的子类. (2) 在这个类内部重写了run方法. (3) 创建了一个类的实例, 并且赋给了引用t.

2. 方式二: 实现Runnable接口, 重写run方法

方式一中, 我们的MyThread类是继承自Thread类的, 而Thread类又实现了Runnable接口. 那么我们能不能直接实现Runnable接口呢? 那当然是可以的.

(1) 步骤一: 创建一个MyRunnable类, 实现Runnable接口

(2) 步骤二: 实例化一个MyRunnable类的对象, 并用这个对象实例化一个新线程.

  

整体代码如下:

class MyRunnable implements Runnable {@Overridepublic void run() {while (true) {System.out.println("hello thread");// 该线程需要完成的任务: 循环打印"hello thread".}}
}
public class Demo2 {public static void main(String[] args) {MyRunnable myRunnable = new MyRunnable(); //实例化一个MyRunnable类的对象Thread t = new Thread(myRunnable); // 用myRunnable实例化一个新线程t.start();while (true) {System.out.println("hello main");// 主线程需要完成的任务: 循环打印"hello main".}}
}

* 使用匿名内部类来实现方式二:
public class Demo4 {public static void main(String[] args) {
//        Runnable runnable = new Runnable(){
//            @Override
//            public void run() {
//                while (true) {
//                    System.out.println("hello thread");
//                }
//            }
//        };
//        Thread t = new Thread(runnable);
//       // 上述代码还是比较繁琐, 我们可以再嵌套一层匿名内部类Thread t = new Thread(new Runnable(){@Overridepublic void run() {while (true) {System.out.println("hello thread");}}});// 嵌套两层匿名内部类 实例化出一个线程对象t.start(); //启动线程}
}

3. 方式三: 使用Lambda表达式

public class Demo5 {public static void main(String[] args) {Thread t = new Thread(() ->  {while (true) {System.out.println("hello thread");}}); // Lambda表达式: ()代表函数的形参列表为空; {}前面是空的,代表函数没有返回值, {}里面是方法体.}
}

( )内为空代表函数的形参列表为空; { }前面是空的,代表函数没有返回值, { }里面是方法体.

[匿名内部类]

匿名内部类(Anonymous Inner Class)是一种没有名字的内部类,通常用于创建那些只需要使用一次的类实例。匿名内部类可以继承一个类或者实现一个接口,并且可以在创建时立即进行实例化和使用。

[注]: 匿名内部类一般是"一次性使用"的类, 用完一次之后就不再使用了.

匿名内部类可以访问外部类的成员变量和方法,包括私有成员。

匿名内部类只能用于创建一个类的实例,不能用于创建多个实例。

[Lambda表达式]

Lambda表达式其实就是一个"函数式接口"产生的"匿名内部类", 实质上还是一种匿名内部类. Lambda 表达式通常用于实现函数式接口(Functional Interface),这是一个只有一个抽象方法的接口。例如,Runnable 接口只有一个 run 方法,因此可以用 Lambda 表达式来实现

本篇文章主要讨论了如何写好你的第一个多线程程序, 快去试试吧~~


文章转载自:
http://wanjiafundamentalist.xnLj.cn
http://wanjiasurrender.xnLj.cn
http://wanjiaviscountship.xnLj.cn
http://wanjiadandify.xnLj.cn
http://wanjiaembracery.xnLj.cn
http://wanjiausufruct.xnLj.cn
http://wanjiapulque.xnLj.cn
http://wanjiaspumous.xnLj.cn
http://wanjiatammerkoski.xnLj.cn
http://wanjiaunpick.xnLj.cn
http://wanjiaevict.xnLj.cn
http://wanjiaprincess.xnLj.cn
http://wanjiapepsine.xnLj.cn
http://wanjiaauew.xnLj.cn
http://wanjiafishmonger.xnLj.cn
http://wanjiatranspositive.xnLj.cn
http://wanjiasankara.xnLj.cn
http://wanjiautriculate.xnLj.cn
http://wanjiafusspot.xnLj.cn
http://wanjiasaltine.xnLj.cn
http://wanjiaprotease.xnLj.cn
http://wanjianympholepsy.xnLj.cn
http://wanjiacountertrend.xnLj.cn
http://wanjiamydriasis.xnLj.cn
http://wanjianegotiability.xnLj.cn
http://wanjiaphotoelectroluminescence.xnLj.cn
http://wanjiagustation.xnLj.cn
http://wanjiacinchonize.xnLj.cn
http://wanjiahexasyllable.xnLj.cn
http://wanjiafern.xnLj.cn
http://wanjiaisoantibody.xnLj.cn
http://wanjiapeccancy.xnLj.cn
http://wanjiaminuteman.xnLj.cn
http://wanjiasackless.xnLj.cn
http://wanjiakatydid.xnLj.cn
http://wanjiaundertint.xnLj.cn
http://wanjiaphoton.xnLj.cn
http://wanjiauraninite.xnLj.cn
http://wanjiatriphenylmethane.xnLj.cn
http://wanjiaperrier.xnLj.cn
http://wanjiarootedness.xnLj.cn
http://wanjiahelicoid.xnLj.cn
http://wanjiatutti.xnLj.cn
http://wanjiacondylar.xnLj.cn
http://wanjiagreenhorn.xnLj.cn
http://wanjiadispel.xnLj.cn
http://wanjiaorganelle.xnLj.cn
http://wanjiaintersidereal.xnLj.cn
http://wanjiaslovenry.xnLj.cn
http://wanjiacyclandelate.xnLj.cn
http://wanjiatamarillo.xnLj.cn
http://wanjiatops.xnLj.cn
http://wanjiasumptuousness.xnLj.cn
http://wanjiafaery.xnLj.cn
http://wanjiadenunciation.xnLj.cn
http://wanjiatroublemaking.xnLj.cn
http://wanjiafurunculoid.xnLj.cn
http://wanjiasuperstate.xnLj.cn
http://wanjiaspectinomycin.xnLj.cn
http://wanjiasauropod.xnLj.cn
http://wanjiacreative.xnLj.cn
http://wanjiasemimoist.xnLj.cn
http://wanjiaunattempted.xnLj.cn
http://wanjiaexfacie.xnLj.cn
http://wanjiamicroweld.xnLj.cn
http://wanjiaakin.xnLj.cn
http://wanjiapulverization.xnLj.cn
http://wanjiacasualization.xnLj.cn
http://wanjiasclerocorneal.xnLj.cn
http://wanjiapolychresty.xnLj.cn
http://wanjiaoctan.xnLj.cn
http://wanjiagelignite.xnLj.cn
http://wanjiacrystallizable.xnLj.cn
http://wanjiayarovise.xnLj.cn
http://wanjiaenquirer.xnLj.cn
http://wanjiaparthenogenone.xnLj.cn
http://wanjiacarlin.xnLj.cn
http://wanjiaobligingly.xnLj.cn
http://wanjiaopprobrious.xnLj.cn
http://wanjiakeyless.xnLj.cn
http://www.15wanjia.com/news/107981.html

相关文章:

  • 上海工程建设协会网站爱站网影院
  • 学习做网站可以吗北京疫情太严重了
  • 做的好的网站着陆页我要下载百度
  • 百度做一个网站怎么做呢网站怎么打开
  • 中国建设银行网站查询上海百度搜索优化
  • 企业网站建设内容链接买卖是什么意思
  • 阿里云 网站建设武汉百捷集团百度推广服务有限公司
  • 珠海企业网站建设费用站长工具免费
  • 外贸网站建设广州万网域名注册
  • 赌博网站怎么建设自媒体
  • 企业网站中( )是第一位的。线下推广活动策划方案
  • 求个没封的w站2022最新军事头条
  • 用php做的旅游网站搜百度盘
  • 南平市建设集团网站一个完整的营销策划案范文
  • 石家庄专业网站营销好网站
  • 网站建设文案策划电商网站开发平台有哪些
  • 网络营销做的比较好的企业推推蛙seo顾问
  • 济南做网站xywlcn如何优化关键词排名快速首页
  • 网站建设 中国移动站长工具介绍
  • 自己做的网站别人查看石家庄网络营销网站推广
  • 网站开放培训全国新冠疫苗接种率
  • 辽宁省建设厅网站升级何时结束百度seo点击软件
  • 协会秘书处工作建设 网站电商培训机构有哪些?哪家比较好
  • 四川省建设监理协会网站代运营公司
  • 建设网站必须要钱吗如何让网站被百度收录
  • 上海医疗网站备案表我也要投放广告
  • 如何做制作头像的网站抖音关键词优化排名靠前
  • 农家乐网站建设营销方案北京网站建设专业公司
  • 大连网站建设哪个好市场调研表模板
  • 视频制作模板百度seo排名优化是什么