`
alexgreenbar
  • 浏览: 87724 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

a simple program reveal thread synchronization

    博客分类:
  • Java
阅读更多
Here's a simple program but reveal thread synchronization mechanism:
- only one thread can enter critical region at a fix time
- two thread enter critical region one by one, and loop
- the time won't affect thread synchronization
- two thread work together like pass ball

package com.demo.thread;

public class DemoThread {	
	public static void main(String[] args) {
		Object obj = new Object();
		
		Working worker1 = new Working("worker-1", obj, 2000);
		Working worker2 = new Working("worker-2", obj, 20);
		
		worker1.start();
		worker2.start();
	}
	
}

class Working extends Thread {
	private String mName;
	private Object mObj;
	private long mTime;
	
	public Working(String name, Object obj, long time) {
		mName = name;
		mObj = obj;
		mTime = time;
	}
	
	public void run() {
		while(true) {
			try {
				System.out.println(mName + " is waiting");
				Thread.sleep(mTime);
				System.out.println(mName + " is trying to enter critical region");
				synchronized(mObj) {
					System.out.println(mName + " has entered into critical region");
					System.out.println(mName + " will notify other thread");
					mObj.notify();
					System.out.println(mName + " will hang");
					mObj.wait();
					System.out.println(mName + " will quit critical region");
				}				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}					
		}		
	}
	
}



the output will like:
worker-1 is waiting
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
.....

分享到:
评论

相关推荐

    Lattice Diamond 3.12 Reveal User Guide

    Lattice Diamond 3.12 Reveal User Guide Lattice Diamond 3.12 Reveal User Guide 是一款高级的 FPGA 片上调试工具,旨在帮助用户更方便地 debug 和优化 FPGA 设计。Reveal 软件提供了几个关键的可用性优势,包括...

    reveal 13 破解版

    reveal 13破解版 666 xcode界面调试工具 6666666666666666666666666666666666666

    reveal2Loader库

    Reveal使用

    Reveal20(11340)破解版

    A new release of Reveal (v20) is available to all users with a current license, and as a 14-day trial. This is a really small release that fixes a couple of issues that we shipped in Reveal 19. The ...

    Reveal 14 Patcher

    Reveal 14 破解工具,直接下载Reveal 14,然后打开Patcher,根据提示操作即可移除试用限制。仅限个人进行研究使用

    Reveal 4 - iOS界面调试神器

    Reveal 是一个界面调试工具。使用Reveal,我们可以在iOS开发时动态地查看和修改应用程序的界面。它类似Chrome的“审查元素”功能,我们不但可以在运行时看到iOS程序的界面层级关系,还可以实时地修改程序界面,不用...

    Reveal 17 破解工具

    Reveal 17 破解工具,请直接从官方网站下载Reveal 17,不要打开Reveal. 然后打开Patcher,根据提示把reveal拖动到Patcher窗口中,看到success字样即代表成功。仅限个人进行研究使用

    reveal 14(10107)

    《深入探索reveal 14(10107):逆向工程与UI调试利器》 reveal 14(10107)是一款强大的iOS应用程序界面(UI)调试工具,专为开发者和设计师设计,它能够帮助用户深入洞察应用程序的用户界面层次结构,实时查看并...

    reveal.js.zip

    **标题:“reveal.js.zip”** **描述:“revealjs”** **标签:“revealjs”** **压缩包子文件的文件名称列表:reveal.js** **正文:** `reveal.js` 是一个开源的HTML演示框架,它允许用户创建交互式、响应式的...

    revealjs-demo

    "revealjs-demo"是一个基于Reveal.js框架的演示文稿示例项目,它通常用于创建交互式的、幻灯片式的演讲或教程。Reveal.js是一个开源的JavaScript库,由Hakim El Hattab开发,它允许开发者用HTML来创建具有动画过渡...

    iOS 调试神器 Reveal

    低版本的 Reveal-SDK,可以使用网上破解版的 Reveal 使用。 缺点:无法使用 Reveal-SDK 的新功能

    reveal.js-master下载

    **标题:“reveal.js-master下载”** **描述:“滚屏式脚本编写 html JavaScript”** **标签:“reveal.js”** 本文将深入探讨`reveal.js`框架,它是一款基于HTML和JavaScript的开源幻灯片制作工具,允许用户...

    Reveal163 破解版

    Reveal163 破解版

    Reveal 4 for Mac 最新版

    Reveal for mac是Mac os平台上的一款开发者调试IOS应用的Mac开发软件,reveal Mac版它能够在运行时调试和修改iOS应用程序。Reveal能连接到应用程序,并允许开发者编辑各种用户界面参数,这反过来会立即反应在程序的...

    Reveal官方使用版

    Reveal是一款强大的第三方iOS UI界面调试工具,它为开发者提供了直观且高效的界面检查和调试能力。本文将详细介绍Reveal的功能、使用方法以及如何通过其免费版来提升开发效率。 首先,Reveal的突出特点在于它的实时...

    Android5.0-RevealEffect动画

    在Android开发中,Reveal Effect是一种非常吸引人的动画效果,特别是在Android 5.0(Lollipop)及更高版本中引入。这种效果通常用于界面切换、按钮点击或视图显示等场景,带给用户更加生动和有趣的交互体验。...

    Reveal使用总结及破解版下载

    Reveal是一款UI调试神器,对iOS开发非常有帮助。废话不多说,这里直接介绍工具的使用以及破解工具下载。另外,本文只适用于调试模拟器,因为调试真机时,需要越狱,这里不做研究。写这片文章之前用的Reveal版本是...

    Reveal17 破解补丁

    Reveal17 破解补丁

    Reveal 1.5.1 破解图文教程

    Reveal 1.5.1 破解图文教程

    Reveal17破解版工具及使用

    Reveal17破解版及使用 1.解压文件 2.打开Reveal 3.打开PYGPatcher.app 4.到官网获取一个使用账号 5.把Reveal拖到PYGPatcher成功破解

Global site tag (gtag.js) - Google Analytics