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

论坛类网站开发报价网站开发语言为wap

论坛类网站开发报价,网站开发语言为wap,谷歌优化seo,代理注册我们知道,在多线程中 Thread thread new Thread(runnable); thread.start();以及 thread.run();都可以执行runnable中run方法下的代码,但是二者又有所不同 下面给出一段代码用以体现二者的区别: 以下代码中,通过thread.start()启…

我们知道,在多线程中
Thread thread = new Thread(runnable);
thread.start();以及 thread.run();都可以执行runnable中run方法下的代码,但是二者又有所不同
下面给出一段代码用以体现二者的区别:
以下代码中,通过thread.start()启动线程,最终产生了线程阻塞

package com.xuecheng;/*** @author Zonda* @version 1.0* @description TODO* @2024/6/15 16:23*/
public class ThreadLocal {public static void main(String[] args) {Runnable runnable = new Runnable() {@Overridepublic void run() {synchronized (this){while(true){try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("打了一发");}}}};Thread thread = new Thread(runnable);thread.start();Thread thread2 = new Thread(runnable);thread2.start();}
}

但如果是调用run方法区启动就不会,这是为什么呢?

        Thread thread = new Thread(runnable);thread.run();Thread thread2 = new Thread(runnable);thread2.run();

因为当我们直接调用run方法执行的时候,这是直接在main方法的主线程中调用run方法,并没有开启一个新的线程,因此 thread.run();和 thread2.run();会在main方法的主线程中顺序执行。这样就不会出现两个线程去争抢同一个锁中的资源的情况。
在这里插入图片描述
而执行start方法会在main线程中异步地开启一个新线程去执行run方法中的代码,如果有两个线程执行start方法,就会出现两个线程同时去执行run方法中的情况。如果一个其中一个线程休眠的时候另一个线程访问这个方法还好,可以交替访问;但是一旦出现一个线程在执行run方法的时候,另一个线程也同时要执行run方法,但是synchronized关键字中的元素只能被一个线程访问,最终会卡死。
在这里插入图片描述
我们通过阅读源码也可以看出只有在调用start方法的时候才会创建线程:
start0();

    public synchronized void start() {/*** This method is not invoked for the main method thread or "system"* group threads created/set up by the VM. Any new functionality added* to this method in the future may have to also be added to the VM.** A zero status value corresponds to state "NEW".*/if (threadStatus != 0)throw new IllegalThreadStateException();/* Notify the group that this thread is about to be started* so that it can be added to the group's list of threads* and the group's unstarted count can be decremented. */group.add(this);
// 将当前线程对象添加到它的线程组。线程组是一种管理线程的机制,可以对线程进行分组管理。boolean started = false;try {start0();started = true;//start0();执行成功,走到这里说明线程创建成功} finally {try {if (!started) {group.threadStartFailed(this);}} catch (Throwable ignore) {/* do nothing. If start0 threw a Throwable thenit will be passed up the call stack */}}}private native void start0();
http://www.yayakq.cn/news/133779/

相关文章:

  • 网站的内容建设中国建设业管理协会网站
  • 公司网站域名如何续费做移动网站优化软
  • 青岛网站建设机构软件工程四大方向
  • 广州建设网站哪家好51网页版在线登录入口
  • j2ee网站开发免费教程wordpress关闭手机访问不了
  • 街道网站建设网站应包括的基本功能和高级功能
  • 给网站写文章怎么做网站的最近浏览 怎么做
  • 网站开发的开发语言棋牌网站哪里做
  • 内网是怎么做网站的互联网行业都有哪些工作岗位呢
  • 建站网站破解版个人备案公司网站
  • 广州专业制作网站wordpress怎么导入产品
  • 杭州营销网站建设公司闵行区网站建设公司
  • 河北建设厅网站初始密码科技创新绘画作品
  • 网站开发的后台开发工具如何制作精美的ppt
  • 网络营销论文题目舟山seo网络优化招聘
  • 如何用文件传输协议登陆网站上海网站建设置作
  • Dw怎么做网站往里面加标题和字网页设计报价单模板
  • 如何寻找做网站的客户修改wordpress登录背景图片
  • 摄影网站模板下载wordpress 4.9 zh cn
  • 公司网页网站建设+ppt模板下载网站连接数据库失败
  • 网站模板设计师要求外国网站英语要求
  • 建行网站宝安住房和建设局网站官网
  • 免费购物网站程序济南网站优化推广
  • 公司网站建设注意点iis wordpress 权限设置
  • 找南昌网站开发公司广州白云住房和建设局网站
  • 网站放自己服务器备案wordpress Ins同步
  • 怎么做网站收录的关键词大连网站哪家做的好?
  • 做窗帘的厂家网站手机端网站制作教程
  • 暴走漫画网站建设中模板企查查网站
  • 网站原型图设计云盘做网站空间