2009年4月15日星期三

Tcl Blend中的线程和event处理

Jacle和Tcl Blend中的tcl.lang.Notifier实现了事件循环处理功能。 一个线程可以包含N个Tcl解释器,在同一个线程中创建的解释器Interp共享同一个Notifer。也就是Notifier是和绑定创建Interp时的线程对应在一起的。最简单的情况,一个线程包含一个Tcl解释器和一个Notifier对象。
看看Interp的构造函数的定义就清楚了:

cThread = Thread.currentThread();
notifier = Notifier.getNotifierForThread(cThread);
notifier.preserve();

Notifier.getNotifierForThread()定义是:

public static synchronized Notifier getNotifierForThread( Thread thread)
{
Notifier notifier = (Notifier) notifierTable.get(thread);
if (notifier == null) {
notifier = new Notifier(thread);
notifierTable.put(thread, notifier);
}

return notifier;
}



通常,事件是在一个循环中通过不断的调用doOneEvent()方法得到处理。
而TclBlend的Event包含了你要完成的逻辑。如果事件队列中没有event,则doOneEvent()会被阻塞,直到获得新的事件。所以,一般创建一个独立的线程来运行Notifier.doOneEvent()。而tcl event都是在其它的线程中创建并通过interp.getNotifier().queueEvent(event, TCL.QUEUE_TAIL) 放到event query中。

调用event.sync(); 会阻塞,直到这个event被处理完毕。

从不是运行doOneEvent()的线程中调用interp.eval()是不合法的,通常导致crash和随机的错误。

If you want to use the tclblend in your java project, you need not only the tclBlend libraries but also the jacl libraries. You should copy the tclBlend and jacl jar files into the same directory otherwise tclBlend it can not work rightly.

没有评论:

发表评论