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

江东外贸seo网站建设怎么创建网站快捷方式

江东外贸seo网站建设,怎么创建网站快捷方式,关闭wordpress多站点,免费网页制作的网站1.当一个同步方法已经执行,线程能够调用对象上的非同步实例方法吗? 可以,一个非同步方法总是可以被调用而不会有任何问题。实际上,Java 没有为非同步方法做任何检查,锁对象仅仅在同步方法或者同步代码块中检查。如果一…

1.当一个同步方法已经执行,线程能够调用对象上的非同步实例方法吗?

可以,一个非同步方法总是可以被调用而不会有任何问题。实际上,Java 没有为非同步方法做任何检查,锁对象仅仅在同步方法或者同步代码块中检查。如果一个方法没有声明为同步,即使你在使用共享数据 Java 照样会调用,而不会做检查是否安全,所以在这种情况下要特别小心。一个方法是否声明为同步取决于临界区访问(critial section access),如果方法不访问临界区(共享资源或者数据结构)就没必要声明为同步的。 

下面有一个示例说明:Common 类有两个方法 synchronizedMethod1()和 method1(),MyThread 类在独立的线程中调用这两个方法。  --------------------- 本文来自 舞动de人生 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/dongwujing/article/details/7647858?utm_source=copy 。

  • public class Common {  
  •   
  • public synchronized void synchronizedMethod1() {  
  • System.out.println (“synchronizedMethod1 called”);  
  • try {  
  • Thread.sleep (1000);  
  • catch (InterruptedException e) {  
  • e.printStackTrace ();  
  • }  
  • System.out.println (“synchronizedMethod1 done”);  
  • }  
  • public void method1() {  
  • System.out.println (“Method 1 called”);  
  • try {  
  • Thread.sleep (1000);  
  • catch (InterruptedException e) {  
  • e.printStackTrace ();  
  • }  
  • System.out.println (“Method 1 done”);  
  • }  
  • }  
  • public class MyThread extends Thread {  
  • private int id = 0;  
  • private Common common;  
  •   
  • public MyThread (String name, int no, Common object) {  
  • super(name);  
  • common = object;  
  • id = no;  
  • }  
  •   
  • public void run () {  
  • System.out.println (“Running Thread” + this.getName ());  
  • try {  
  • if (id == 0) {  
  • common.synchronizedMethod1();  
  • else {  
  • common.method1();  
  • }  
  • catch (Exception e) {  
  • e.printStackTrace ();  
  • }  
  • }  
  •   
  • public static void main (String[] args) {  
  • Common c = new Common ();  
  • MyThread t1 = new MyThread (“MyThread-1″, 0, c);  
  • MyThread t2 = new MyThread (“MyThread-2″, 1, c);  
  • t1.start ();  
  • t2.start ();  
  • }
  • }

输出结果

  • Running ThreadMyThread-1  
  • synchronizedMethod1 called  
  • Running ThreadMyThread-2  
  • Method 1 called  
  • synchronizedMethod1 done  
  • Method 1 done 

--------------------- 本文来自 舞动de人生 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/dongwujing/article/details/7647858?utm_source=copy 

2.在一个对象上两个线程可以调用两个不同的同步实例方法吗? 

不能,因为一个对象已经同步了实例方法,线程获取了对象的对象锁。所以只有执行完该方法释放对象锁后才能执行其它同步方法。看下面代码示例非常清晰:Common 类有 synchronizedMethod1()和 synchronizedMethod2()方法,MyThread 调用这两个方法。

  • public class Common {  
  • public synchronized void synchronizedMethod1() {  
  • System.out.println (“synchronizedMethod1 called”);  
  • try {  
  • Thread.sleep (1000);  
  • catch (InterruptedException e) {  
  • e.printStackTrace ();  
  • }  
  • System.out.println (“synchronizedMethod1 done”);  
  • }  
  •   
  • public synchronized void synchronizedMethod2() {  
  • System.out.println (“synchronizedMethod2 called”);  
  • try {  
  • Thread.sleep (1000);  
  • catch (InterruptedException e) {  
  • e.printStackTrace ();  
  • }  
  • System.out.println (“synchronizedMethod2 done”);  
  • }  
  • }  
  • public class MyThread extends Thread {  
  • private int id = 0;  
  • private Common common;  
  •   
  • public MyThread (String name, int no, Common object) {  
  • super(name);  
  • common = object;  
  • id = no;  
  • }  
  •   
  • public void run () {  
  • System.out.println (“Running Thread” + this.getName ());  
  • try {  
  • if (id == 0) {  
  • common.synchronizedMethod1();  
  • else {  
  • common.synchronizedMethod2();  
  • }  
  • catch (Exception e) {  
  • e.printStackTrace ();  
  • }  
  • }  
  •   
  • public static void main (String[] args) {  
  • Common c = new Common ();  
  • MyThread t1 = new MyThread (“MyThread-1″, 0, c);  
  • MyThread t2 = new MyThread (“MyThread-2″, 1, c);  
  • t1.start ();  
  • t2.start ();  
  • }  
  • }  

----------

输出结果

Running ThreadMyThread-1
Running ThreadMyThread-2
synchronizedMethod1 called
synchronizedMethod1 done
synchronizedMethod2 called
synchronizedMethod2 done

 

----------- 本文来自 舞动de人生 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/dongwujing/article/details/7647858?utm_source=copy 

转载于:https://www.cnblogs.com/toby-ma/p/9754403.html

http://www.lbrq.cn/news/2616913.html

相关文章:

  • 大兴网站建设优化seo百度搜索引擎优化方案
  • 东莞微网站建设报价黑马教育培训官网
  • 怎么在虚拟机中做网站培训seo哪家学校好
  • 简述网站制作的流程seo技术培训沈阳
  • 政府网站为什么要改版2024年1月新冠高峰
  • 中英网站怎么做自己做网站怎么做
  • 济宁网站建设seo长尾关键词排名
  • 阿盟住房与建设局门户网站查权重
  • 怎样免费做自己的网站我为什么不建议年轻人做销售
  • 乌克兰武装部队最新战报站长工具seo综合查询论坛
  • 西樵网站开发兰州网络seo
  • 株洲网站建设优化企业网络营销策划书1500字
  • 网站域名年费网站推广计划书范文500字
  • 东营市做网站的公司小程序开发公司
  • 中国建筑公司排名前20seo课培训
  • 做微博类的网站难吗长沙正规竞价优化服务
  • wordpress购物网站手机网站优化网
  • 南京网站制作平台济南seo网络优化公司
  • 网站地图在线生成器优化大师使用心得
  • 织梦网站入侵网站备案流程
  • 有哪些能做专门接做标书的网站如何找做网站的公司
  • 携程旅行的网站建设合肥网站建设
  • 昆明企业网站建设重庆可靠的关键词优化研发
  • 专业佛山网站建设seo教程下载
  • 网站后台如何开发青岛网站推广系统
  • 铜梁网站建设合肥seo推广排名
  • 信息爆炸的时代做网站seo关键词怎么选择
  • 如何购买一个网站的域名搜索引擎营销的特点包括
  • 芜湖 网站建设市场策划方案
  • wordpress内容清空南宁关键词优化公司
  • 原型模式在C++中的实现与面向对象设计原则
  • Laravel The requested URL /hellowzy was not found on this server. 404 问题的解决
  • 【Linux系统】进程间通信:基于匿名管道实现进程池
  • LeetCode 132:分割回文串 II
  • 思途Spring学习 0804
  • Python实现Word转PDF全攻略:从入门到实战