[Tcl Java] Re: Thread safety (was: Using JACL in servlets)
On Mon, 1 May 2000, Jeff Sturm wrote: Mo DeJong wrote: The term "thread safe" is very misleading. Jacl interps are safe if you use them properly. The problem is that the documentaiton about how to use them properly is a little thin. There is no "automatic thread safety" in TclJava. In fact, Java itself never got "automatic thread safety" completely right. The AWT was a great example of how not to write a GUI (with lots of "automatic" thread locking). You could say the same about most Java code. But I agree the term "thread safe" is not very precise. I was referring to automatic thread safety... which, depending on the project, there may be good reasons to shun for performance reasons (Swing is a good example). It does. The docs say that ALL methods that interact with an interp instance and thread unsafe. For this reason, all interp interaction needs to be done from the Tcl event queue thread. Here is a quick example that call the "unsafe" eval() method using the Notifier to queue up a "thread safe" event. import tcl.lang.*; public class EventExample { // Interact with the Tcl interp (must be called from event loop thread) void setXto1 (Interp interp) throws TclException { interp.eval("set x 1"); } // This method can be safely called from any thread public void SAFE_setXto1(final Interp interp) { TclEvent event = new TclEvent() { public int processEvent (int flags) { try { setXto1(interp); } catch (TclException ex) { // Do seomthing to handle the error } return 1; } }; interp.getNotifier().queueEvent(event, TCL.QUEUE_TAIL); } } Note how the setXto1() method is called from the interp event queue thread (the paper does a better job of explaining this). The API docs should at least specify which calls are safe to use concurrently and which are not. A new paper on the subject of multi-threaded interaction with a TclJava interp is being authored by Jiang Wu [EMAIL PROTECTED]. I am sure he will publish the URL on this list when he thinks it is ready (If you want to help out, I am sure Jiang could always use another pair of eyes to help review the paper). Cool, shoot Jiang an email, I am sure he would welcome the help. Good. I'd welcome a chance to review it. -- Jeff Sturm [EMAIL PROTECTED] Mo Dejong Red Hat Inc. The TclJava mailing list is sponsored by Scriptics Corporation. To subscribe:send mail to [EMAIL PROTECTED] with the word SUBSCRIBE as the subject. To unsubscribe: send mail to [EMAIL PROTECTED] with the word UNSUBSCRIBE as the subject. To send to the list, send email to '[EMAIL PROTECTED]'. An archive is available at http://www.mail-archive.com/tcljava@scriptics.com
[Tcl Java] RE: [Tcl Java] Re: Thread safety (was: Using JACL in servlets)
You have a good point. The shell is a special case that takes a shortcut when used with a file argument. In the case of a file argument, there is only one thread for the Shell. The console thread is never created. As a result, it is valid for the main thread to call interp.evalFile() without using an event queue. However, I think this is a corner case because a large number of Java application cannot get away with using only a single thread. When the Shell must take user input interactively, it needs a second thread to do this. Then, the event queue mechanism must be used. I think for most Java application that needs to embed the Tcl interpreter, there are more than one thread at work. The basic concept is that the Tcl interpreter is designed for single thread execution. If you have only one thread, you can call any methods of the interpreter at any time in this thread. If you have more than one thread, use the event queue. You can also implement the Shell with the file argument using the event loop method by using 2 threads. One runs the event queue, and the other sends "source filename", then sends "exit" to the event queue. -- Jiang Wu [EMAIL PROTECTED] -Original Message- From: Jeff Sturm [mailto:[EMAIL PROTECTED]] Sent: Monday, May 01, 2000 1:51 PM To: Mo DeJong Cc: [EMAIL PROTECTED] Subject: [Tcl Java] Re: Thread safety (was: Using JACL in servlets) Mo DeJong wrote: It does. The docs say that ALL methods that interact with an interp instance and thread unsafe. For this reason, all interp interaction needs to be done from the Tcl event queue thread. Here is a quick example that call the "unsafe" eval() method using the Notifier to queue up a "thread safe" event. I don't see the event loop used consistently. The tcl.lang.Shell in CVS for example never runs an event loop when run with a filename argument: java tcl.lang.Shell hello.tcl -- Jeff Sturm [EMAIL PROTECTED] The TclJava mailing list is sponsored by Scriptics Corporation. To subscribe:send mail to [EMAIL PROTECTED] with the word SUBSCRIBE as the subject. To unsubscribe: send mail to [EMAIL PROTECTED] with the word UNSUBSCRIBE as the subject. To send to the list, send email to '[EMAIL PROTECTED]'. An archive is available at http://www.mail-archive.com/tcljava@scriptics.com The TclJava mailing list is sponsored by Scriptics Corporation. To subscribe:send mail to [EMAIL PROTECTED] with the word SUBSCRIBE as the subject. To unsubscribe: send mail to [EMAIL PROTECTED] with the word UNSUBSCRIBE as the subject. To send to the list, send email to '[EMAIL PROTECTED]'. An archive is available at http://www.mail-archive.com/tcljava@scriptics.com