Created
April 3, 2015 16:52
-
-
Save ChinaXing/aaaf6cab0e32d6094667 to your computer and use it in GitHub Desktop.
message passing like channel use object monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.witown.probe.stream.storm; | |
import org.junit.Test; | |
import sun.awt.windows.ThemeReader; | |
/** | |
* Created by LambdaCat on 15/4/4. | |
*/ | |
public class TestCSP { | |
private static Integer x = 0; | |
@Test | |
public void Test() { | |
Thread a = new Thread(new Runnable() { | |
public void run() { | |
synchronized (TestCSP.class) { | |
while (true) { | |
try { | |
Thread.sleep(1000); | |
TestCSP.class.wait(); | |
System.out.println("X = " + x); | |
TestCSP.class.notify(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
}); | |
Thread b = new Thread(new Runnable() { | |
public void run() { | |
synchronized (TestCSP.class) { | |
while (true) { | |
try { | |
x = x + 1; | |
TestCSP.class.notify(); | |
Thread.sleep(500); | |
System.out.println("b alive after notify .."); | |
TestCSP.class.wait(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
}); | |
a.start(); | |
b.start(); | |
try { | |
Thread.sleep(Integer.MAX_VALUE); | |
} catch (Exception e) { | |
} | |
} | |
} |
设计一种非分时的系统。系统线程、进程的切换完全由应用程序来控制,这样减少context switch,一定会高效。
逻辑调度器负责这种逻辑线程的切换,每当要暂停自己的时候,通知调度器,调度器挂起并记录,然后唤起等待条件的进程。
逻辑调度器在一个进程或者线程中运行,那么对于运行其的系统,完全是一个完成的线程,没有切换。
但是逻辑上讲,线程的内部完成了一种调度——这种调度是逻辑上的控制流的切换。
内涵于逻辑调度器及其被调度”线程“
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这里就是一种coroutine的情形?
协作方式的好处是减少不必要的切换,按需切换。