栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java-同步静态方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java-同步静态方法

这是我的测试代码,表明您是正确的,并且本文有点过分谨慎:

class Y {    static synchronized void staticSleep() {        System.out.println("Start static sleep");        try { Thread.sleep(2000);        } catch (InterruptedException e) {        }        System.out.println("End static sleep");    }    synchronized void instanceSleep() {        System.out.println("Start instance sleep");        try { Thread.sleep(200);        } catch (InterruptedException e) {        }        System.out.println("End instance sleep");    }}public class X {    public static void main(String[] args) {        for (int i = 0; i < 2; ++i) { new Thread(new Runnable() {     public void run() {         Y.staticSleep();     } }).start();        }        for (int i = 0; i < 10; ++i) { new Thread(new Runnable() {     public void run() {         new Y().instanceSleep();     } }).start();        }    }}

印刷品:

Start instance sleepStart instance sleepStart instance sleepStart instance sleepStart instance sleepStart static sleepStart instance sleepStart instance sleepStart instance sleepStart instance sleepStart instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd instance sleepEnd static sleepStart static sleepEnd static sleep

因此与实例

static synchronized
synchronized
方法无关…

当然,如果

static synchronised
整个系统都使用这些方法,那么您可以期望它们对多线程系统的吞吐量产生最大的影响,因此请自担风险。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/453948.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号