Java中的同步——示例程序

Java中的同步——示例程序,第1张

前面说到了Java中的同步问题 下面通过一个小小的实例程序来演示Java中的同步方法 其中对前文提到的Counter类做了稍微的修改

public class Counter {

private int c =

public void increment() {

System out println( before increment c = + c)

c++

try {

Thread sleep( )

} catch (InterruptedException e) {

e printStackTrace()

}

System out println( after increment c = + c)

}

public  void decrement() {

System out println( before decrement c = + c)

c

try {

Thread sleep( )

} catch (InterruptedException e) {

e printStackTrace()

}

System out println( after decrement c = + c)

}

public int value() {

return c

}

}

在上面的Counter类的实现中 分别对increment和decrement方法中增加了sleep( )的调用 这样做的目的是为了放大两个线程对同一对象的方法调用时的交错效果

下面是两个线程 在ThreadA中调用了 次increment()方法 在ThreadB中调用了 次decrement()方法

Thread

public class ThreadA implements Runnable {

private Counter c

public ThreadA(Counter c) {

this c = c

}

@Override

public void run() {

for (int i = i <i++) {

this c increment()

}

}

}

ThreadB

public class ThreadB implements Runnable {

private Counter c

public ThreadB(Counter c) {

this c = c

}

@Override

public void run() {

for (int i = i <i++) {

this c decrement()

}

}

}

主程序如下 其中生成了两个线程threadA和ThreadB 他们共享Counter c

public class Main {

public static void main(String[] args) {

Counter c = new Counter()

ThreadA a = new ThreadA(c)

ThreadB b = new ThreadB(c)

Thread threadA = new Thread(a)

Thread threadB = new Thread(b)

threadA start()

threadB start()

}

}

执行上面的代码 可能的结果如下

before increment c =

before decrement c =

after increment c =

before increment c =

after decrement c =

before decrement c =

after increment c =

before increment c =

after decrement c =

before decrement c =

after increment c =

before increment c =

after decrement c =

before decrement c =

after increment c =

before increment c =

after decrement c =

before decrement c =

after increment c =

before increment c =

after decrement c =

before decrement c =

after increment c =

before increment c =

after increment c =

before increment c =

after decrement c =

before decrement c =

after decrement c =

before decrement c =

after increment c =

before increment c =

after increment c =

before increment c =

after decrement c =

before decrement c =

after increment c =

after decrement c =

before decrement c =

after decrement c =

从上面的输出结果中我们不难看出出现了严重的交错现象! 在increment或者是decrement方法中输出before和after本应该是成对连续出现的 但输出结果却不是如此

将上面代码的increment()和decrement()方法用synchronized 修饰后 重新运行该程序 输出结果如下

before increment c =

after increment c =

before increment c =

after increment c =

before decrement c =

after decrement c =

before increment c =

after increment c =

before decrement c =

after decrement c =

before decrement c =

after decrement c =

before decrement c =

after decrement c =

before decrement c =

after decrement c =

before increment c =

after increment c =

before decrement c =

after decrement c =

before decrement c =

after decrement c =

before decrement c =

after decrement c =

before decrement c =

after decrement c =

before increment c =

after increment c =

before decrement c =

after decrement c =

before increment c =

after increment c =

before increment c =

after increment c =

before increment c =

after increment c =

before increment c =

after increment c =

before increment c =

after increment c =

这样输出结果和没有增加synchronized修饰符时的大不相同 单独一次的increment和decrement方法并没有出现交错的现象 只是连续 次的increment()和decrement ()有交错(这个不是synchronized能解决的问题)

至少 我们从上面的实例程序中可以看到synchronized方法的作用了

lishixinzhi/Article/program/Java/hx/201311/26369

1、打开编程猫手机客户端编程猫nemo,出现界面,界面上会显示系统带的示例程序以及近期制作的一些程序,点击屏幕下方的创作进行编程设计。

2、打开创作之后进入如图所示界面。左侧是可能用到的工具栏,右侧是预览界面,左侧工具栏添加角色,添加背景,选择积木按钮,就可以对相应的角色或者背景进行编程。

3、点击积木按钮之后,出现编程界面,在右侧是对应的功能模块,点击模块会出现相应的子选项,根据需要将积木模块拖动到编程区域。

4、第一个人物程序设置完成之后,点击最左侧上的如图所示切换选项,点击倒三角会出现创建的所有角色以及背景,选中之后就会切换到对应的人物编程界面。

5、在使用积木的过程中,如果需要删除模块,需要将模块拖动到手机右上角,这里有一个隐藏的回收站,将模块拖动到附近区域的时候,回收站就会显示出现,当回收站显示成红色时松手,模块就被删除。长按模块也可以进行复制粘贴 *** 作。

6、设置完成之后,点击屏幕左下角的橙色播放按钮,就开始程序运行,运行之后,左下角停止按钮可以终止程序运行。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11030722.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存