`
alexgreenbar
  • 浏览: 85406 次
  • 性别: 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
.....

分享到:
评论

相关推荐

    reveal 13 破解版

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

    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.js.zip

    revealjs

    Reveal 4 - iOS界面调试神器

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

    Reveal 17 破解工具

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

    reveal2Loader库

    Reveal使用

    iOS 调试神器 Reveal

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

    Reveal163 破解版

    Reveal163 破解版

    Lattice Diamond 3.12 Reveal User Guide

    Reveal User Guide 的机翻中文版

    Reveal使用总结及破解版下载

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

    Reveal 4 for Mac 最新版

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

    Reveal17 破解补丁

    Reveal17 破解补丁

    Reveal 1.5.1 破解图文教程

    Reveal 1.5.1 破解图文教程

    Reveal17破解版工具及使用

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

    Reveal 21 Mac 破解版

    Reveal 21 Mac 破解版 可查看app界面结构,方便逆向其他app。 1.打开dmg镜像,将“Reveal.app”拖入应用程序中; 2.双击"Reveal20_Patcher",将应用程序中的“Reveal.app”拖入等待提示成功; 3.打开应用程序中的...

    Reveal-master

    Reveal破解版

    Reveal各版本通用破解工具

    Reveal各版本通用破解工具 使用方法: 将Reveal放到应用程序里,然后运行RevealCracker,点击按钮就行了。 如果有购买能力的话还是支持使用正版。

    Reveal14破解工具

    reveal破解软件。只适用于14版本,不适合其他版本,敬请下载

    Reveal 破解版ios逆向调试工具

    Reveal 破解版ios逆向调试工具,ios逆向开发ui调试工具

Global site tag (gtag.js) - Google Analytics