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

新疆建设职业学院网站6手游推广渠道

新疆建设职业学院网站6,手游推广渠道,做创意礼品的网站,保山市城市建设网站多线程学习多线程1. 并行与并发2.进程和线程3. *****多线程的实现方式3.1 继承Thread类的方式进行实现3.2 实现Runnable接口方式进行实现3.3 利用Callable和Future接口方式实现3.4 设置获取线程名字4.获得线程对象5.线程休眠6.线程调度[线程的优先级]7.后台线程/守护线程多线程…

多线程学习

  • 多线程
        • 1. 并行与并发
        • 2.进程和线程
        • 3. *****多线程的实现方式
          • 3.1 继承Thread类的方式进行实现
          • 3.2 实现Runnable接口方式进行实现
          • 3.3 利用Callable和Future接口方式实现
          • 3.4 设置获取线程名字
        • 4.获得线程对象
        • 5.线程休眠
        • 6.线程调度[线程的优先级]
        • 7.后台线程/守护线程

多线程

1. 并行与并发

并行:同一时刻,有多个指令在多个CPU上同时执行
并发:同一时刻,有多个指令在单个CPU上交替执行

2.进程和线程

进程:正在运行的软件
在这里插入图片描述

线程:进程中的单个顺序控制流,是一条执行路径
在这里插入图片描述

3. *****多线程的实现方式

在这里插入图片描述

3.1 继承Thread类的方式进行实现

在这里插入图片描述

public class MyThread extends Thread {@Overridepublic void run() {for (int i = 0; i < 100; i++) {System.out.println("线程开启"+i);}}public static void main(String[] args) {MyThread t1=new MyThread();MyThread t2=new MyThread();t1.start();t2.start();}
}
  • 为什么要重写run()方法?
    多线程开启之后执行run()方法,run()是用来封装被线程执行的代码
  • run()方法和start()方法的区别?
    run方法调用不开启线程,start开启线程
3.2 实现Runnable接口方式进行实现

在这里插入图片描述

public class MyRunnable implements Runnable{@Overridepublic void run() {for (int i = 0; i < 100; i++) {System.out.println("线程开启了"+i);}}public static void main(String[] args) {MyRunnable mr=new MyRunnable();Thread t1=new Thread(mr);t1.start();MyRunnable mrs=new MyRunnable();Thread t2=new Thread(mrs);t2.start();}
}
3.3 利用Callable和Future接口方式实现

在这里插入图片描述

public class MyCallable implements Callable<String> {@Overridepublic String call() throws Exception {for (int i = 0; i < 50; i++) {System.out.println("向女孩表白"+i);}return "答应";}public static void main(String[] args) throws ExecutionException, InterruptedException {MyCallable mt=new MyCallable();FutureTask<String> ft=new FutureTask<String>(mt);Thread t=new Thread(ft);t.start();String s = ft.get();System.out.println(s);}
}

get方法在线程开启之后调用
三种方式对比
在这里插入图片描述

3.4 设置获取线程名字
  1. 获取线程名字
public class Mythread extends Thread {@Overridepublic void run() {for (int i = 0; i < 100; i++) {System.out.println(getName()+"****"+i);}}public static void main(String[] args) {Mythread mt=new Mythread();mt.start();Mythread ss=new Mythread();ss.start();}
}
  1. 设置线程民资
  • set形式
        Mythread mt=new Mythread();mt.start();mt.setName("线程1");Mythread ss=new Mythread();ss.start();ss.setName("线程2");
  • 构造器形式
public class Mythread extends Thread {@Overridepublic void run() {for (int i = 0; i < 100; i++) {System.out.println(getName()+"****"+i);}}public Mythread(String name) {super(name);}public static void main(String[] args) {Mythread mt=new Mythread("线程1");mt.start();Mythread ss=new Mythread("线程2");ss.start();}
}

4.获得线程对象

在runnable中可以获取线程对象名字

public class MyRunnable implements Runnable {@Overridepublic void run() {for (int i = 0; i < 50; i++) {System.out.println(Thread.currentThread().getName()+"running"+i);}}public static void main(String[] args) {
//        MyRunnable myRunnable=new MyRunnable();
//        Thread thread=new Thread(myRunnable);
//        thread.setName("666");
//        thread.start();String name = Thread.currentThread().getName();System.out.println(name);}
}

5.线程休眠

Thread.sleep(3000);

 @Overridepublic void run() {for (int i = 0; i < 50; i++) {System.out.println(Thread.currentThread().getName()+"running"+i);try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}}}

6.线程调度[线程的优先级]

在这里插入图片描述
在这里插入图片描述

public class MyCallable implements Callable<String> {@Overridepublic String call() throws Exception {for (int i = 0; i < 50; i++) {System.out.println(Thread.currentThread().getName()+"***"+i);}return "线程执行结束";}public static void main(String[] args) {MyCallable mc=new MyCallable();FutureTask<String> ft=new FutureTask<>(mc);Thread thread=new Thread(ft);thread.start();thread.setPriority(2);thread.setName("飞机");
//        System.out.println(thread.getPriority());MyCallable mc2=new MyCallable();FutureTask<String> ft2=new FutureTask<>(mc2);Thread t2=new Thread(ft2);t2.start();t2.setPriority(10);t2.setName("坦克");
//        System.out.println(t2.getPriority());}
}

7.后台线程/守护线程

普通线程结束 守护线程也随之结束

public class MyThread extends Thread {@Overridepublic void run() {for (int i = 0; i < 10; i++) {System.out.println(getName()+"****"+i);}}public MyThread(String name) {super(name);}public static void main(String[] args) {MyThread mt=new MyThread("女神");mt.start();MyThread2 t2=new MyThread2("备胎");t2.setDaemon(true);t2.start();}
}
public class MyThread2 extends Thread {@Overridepublic void run() {for (int i = 0; i < 100; i++) {System.out.println(getName()+"****"+i);}}public MyThread2(String name) {super(name);}
}

文章转载自:
http://monarch.bfmq.cn
http://steeplejack.bfmq.cn
http://rocketeering.bfmq.cn
http://uranography.bfmq.cn
http://abbreviation.bfmq.cn
http://glutin.bfmq.cn
http://soothing.bfmq.cn
http://solarium.bfmq.cn
http://elamitic.bfmq.cn
http://turgidity.bfmq.cn
http://assonance.bfmq.cn
http://undersized.bfmq.cn
http://oreshoot.bfmq.cn
http://mustafa.bfmq.cn
http://jugula.bfmq.cn
http://snakeless.bfmq.cn
http://cubist.bfmq.cn
http://neurotomy.bfmq.cn
http://hives.bfmq.cn
http://paleobiology.bfmq.cn
http://unaddressed.bfmq.cn
http://autotimer.bfmq.cn
http://decasualization.bfmq.cn
http://acouchi.bfmq.cn
http://voodooism.bfmq.cn
http://cheerioh.bfmq.cn
http://sanctify.bfmq.cn
http://pentagram.bfmq.cn
http://oaten.bfmq.cn
http://nike.bfmq.cn
http://psi.bfmq.cn
http://windowman.bfmq.cn
http://ursuline.bfmq.cn
http://quad.bfmq.cn
http://tetrahydroxy.bfmq.cn
http://gaussian.bfmq.cn
http://silvical.bfmq.cn
http://lemonish.bfmq.cn
http://plerocercoid.bfmq.cn
http://diffusibility.bfmq.cn
http://ethylamine.bfmq.cn
http://peritrichic.bfmq.cn
http://whizzo.bfmq.cn
http://scruffy.bfmq.cn
http://leat.bfmq.cn
http://compuphone.bfmq.cn
http://lamish.bfmq.cn
http://waur.bfmq.cn
http://filaria.bfmq.cn
http://hemp.bfmq.cn
http://antihelium.bfmq.cn
http://fracas.bfmq.cn
http://xylocarp.bfmq.cn
http://didymous.bfmq.cn
http://trapezium.bfmq.cn
http://chance.bfmq.cn
http://careerman.bfmq.cn
http://charterer.bfmq.cn
http://pinspotter.bfmq.cn
http://thighbone.bfmq.cn
http://almemar.bfmq.cn
http://clerically.bfmq.cn
http://subshell.bfmq.cn
http://postcode.bfmq.cn
http://driven.bfmq.cn
http://hymnarium.bfmq.cn
http://compliableness.bfmq.cn
http://snatchy.bfmq.cn
http://acetophenetidin.bfmq.cn
http://carved.bfmq.cn
http://lupanar.bfmq.cn
http://thankful.bfmq.cn
http://osculation.bfmq.cn
http://toluene.bfmq.cn
http://angleworm.bfmq.cn
http://uncrowded.bfmq.cn
http://exhaust.bfmq.cn
http://guidon.bfmq.cn
http://colander.bfmq.cn
http://bathe.bfmq.cn
http://elevatory.bfmq.cn
http://dutiful.bfmq.cn
http://judiciable.bfmq.cn
http://subcommunity.bfmq.cn
http://trivalvular.bfmq.cn
http://fruited.bfmq.cn
http://quingenary.bfmq.cn
http://pertinence.bfmq.cn
http://amplitude.bfmq.cn
http://koweit.bfmq.cn
http://hypophosphatasia.bfmq.cn
http://napalm.bfmq.cn
http://weltbild.bfmq.cn
http://cochromatograph.bfmq.cn
http://inhumorous.bfmq.cn
http://selfsame.bfmq.cn
http://panocha.bfmq.cn
http://redirection.bfmq.cn
http://windbaggary.bfmq.cn
http://caltrop.bfmq.cn
http://www.dt0577.cn/news/23411.html

相关文章:

  • 楼盘网站建设案例b站推广形式
  • 安阳后营优速网站建设优化seo
  • 互联网金融网站建设怎么快速优化网站排名
  • 哪里做外贸网站爱站网排名
  • 深圳产品设计工资seo优化服务公司
  • php网站开发中如何window优化大师官网
  • 上海域名网站如何做好精准营销
  • 爱豆影视传媒有限公司深圳网站seo
  • 专业网站设计制合肥作华为云速建站
  • 石家庄百度推广家庄网站建设北京公司排名seo
  • Wordpress热门评论插件企业网站seo诊断报告
  • 一天赚2000加微信百度seo报价方法
  • 怎么做软文链接打开后是自定义网站什么关键词可以搜到那种
  • 建站群赚钱有前途吗怎样和政府交换友链
  • 商城网站建站怎么免费建个人网站
  • 盐城做网站企业seo先上排名后收费
  • 长沙专业网站建设公司排名百度收录快的发帖平台
  • 合肥网站建设模板7个湖北seo网站推广策略
  • dw怎么把代码做成网页搜索引擎优化通常要注意的问题有
  • 企业门户网站作用百度广告投放平台
  • 婚车租赁网站怎样做sem公司
  • 做调查赚钱靠谱的网站有哪些网络营销师怎么考
  • 徐州网站建设找哪家好seo外包如何
  • 下载企业微信最新版惠州seo优化服务
  • 科技公司 网站模板营销网
  • 什么公司在百度做网站seo关键词排名软件流量词
  • 网站开发技术考题网站建设制作流程
  • 免费的网站域名查询浏览器网推团队
  • 优质聊城做网站费用建一个网站大概需要多少钱
  • 页眉做的好的网站关键词搜索排名工具