Wednesday, June 8, 2011

programmatic control on a thread

Update: The Process object in dotnet is another "handle" on a OS construct...

Suppose a virtual machine thread FastRunner is linked to a Thread Object TO2.
A) As a java object, a Thread.java object TO2 is a chunk of HEAP mem, and can have fields.
B) As a real thread ie "vm thread", FastRunner occupies memory for its call stack and can receive cpu cycles.
In general, a call stack can have "access" to any heap object if you pass in a (indirect) reference. TO2 is a heap object and can be accessed on the call stack of FastRunner. In fact it's dead easy. Thread.currentThread() will return a TO2 reference. This is a embrace.
1) TO2 has a pointer to the call stack of FastRunner and can affect FastRunner;
2) among the local variables of the call stack, there's now a pointer to TO2.
A field f1 in TO2 is not related to anything in the vm thread FastRunner. You might potentially want to use TO2 fields to control FastRunner, but TO2.f1 is just like a field in any heap object.
Fundamentally, TO2 has a pointer to the FastRunner vm thread. JVM provides no reference to FastRunner as it's not on the heap and not a java Object.
Suppose you have a (usually stateless) Runnable object R2. It is basically a function pointer. R2 has nothing but 4 bytes holding the address of run() method which lives in Code Section, not stack not heap. You can pass R2 to Thread() ctor to create TO2 object. TO2 has a different address than R2 though.
If TO2 is a television remote control, then it offers a small number of "buttons" to control FastRunner, like
* interrupt()
* join()
* start(). Note run() should never be called except by start()
- if you have a java reference to the TO2 object, you can't use it to make FastRunner sleep
- if you have a java reference to the TO2 object, you can't use it to make FastRunner grab a lock
- if you have a java reference to the TO2 object, you can't use it to make FastRunner wait in a monitor
- if you have a java reference to the TO2 object, you can't use it to notify FastRunner
I'd say the remote control is so limited that most of the important "buttons" are missing. As explained below, TO2 is a "poor" handle on the vm thread (FastRunner) known as a "thread" with its call stack, thread registers, cpu cycles etc. Better to avoid confusion -- distinguish "Thread" and "thread". P27 [[java threads]] explains
* when a vm thread in VM is running its run() method, both the VM thread and the Thread object are connected. TO2 is a relatively good handle on the vm thread in VM.
* after run() returns, vm thread no longer gets cpu cycles, but the Thread object is still useful until it's garbage collected.
* after you instantiate a Thread, but before you call its start(), the call stack doesn't exist.
* bottom line -- Thread object lives a bit longer than a vm thread.

If you naively think that an instance method in TO2 is related to the corresponding call stack of FastRunner, then you are wrong on multiple accounts.
* if FastRunner run() method calls such a method m1(), then the method runs on FastRunner, but m1() body may have nothing to do with the VM thread FastRunner.
* As explained earlier, Thread has longer lifespan than thread, so any method call made outside FastRunner's lifespan is unrelated to FastRunner the vm thread.
* the main thread can call any of those methods. Such method calls may have nothing to do with the VM thread's call stack.
In terms of programmatic control on the vm thread, D) is most useful, followed by B and C --
C) thread instance methods -- least used. Quiz -- can you name 2?
TO2.interrupt() is one of the few important instance methodS. Others include run(), join() and the associted isAlive(),
B) Thread.java static methods let you probe/modify the thread to some extent, such AS Thread.sleep(). These Thread methods are static methods, without a specific Thread instance as the target. They usually affect the current thread. The static and non-static mix in Thread.java is confusing. I feel these should be put into a ThreadControl.java class and converted to instance methods, and instantiated as a static member of System, just like System.out.
D) Many of the modifications, controls && operations on the thread are effected through constructs outside the Thread instance, such as wait(), the all-important keyword synchronized, which is similar to the Lock.lock() ...
There's something like TO2.isInterrupted() instance(!) method which is often called -- Thread.currentThread().isInterrupted().

No comments:

Total Pageviews

my favorite topics (labels)

_fuxi (302) _misLabel (13) _orig? (3) _rm (2) _vague (2) clarified (58) cpp (39) cpp_const (22) cpp_real (76) cpp/java/c# (101) cppBig4 (54) cppSmartPtr (35) cppSTL (33) cppSTL_itr (27) cppSTL_real (26) cppTemplate (28) creditMkt (14) db (65) db_sybase (43) deepUnder (31) dotnet (20) ECN (27) econ/bank` (36) fin/sys_misc (43) finGreek (34) finReal (45) finRisk (30) finTechDesign (46) finTechMisc (32) finVol (66) FixedIncom (28) fMath (7) fMathOption (33) fMathStoch (67) forex (39) gr8IV_Q (46) GTD_skill (15) GUI_event (30) inMemDB (42) intuit_math (41) intuitFinance (57) javaMisc (68) javaServerSide (13) lambda/delegate (22) marketData (28) math (10) mathStat (55) memIssue (8) memMgmt (66) metaProgram` (6) OO_Design (84) original_content (749) polymorphic/vptr (40) productive (21) ptr/ref (48) py (28) reflect (8) script`/unix (82) socket/stream (39) subquery/join (30) subvert (13) swing/wpf (9) sysProgram` (16) thread (164) thread_CAS (15) thread_cpp (28) Thread* (22) timeSaver (80) transactional (23) tune (24) tuneDB (40) tuneLatency (30) z_ajax (9) z_algoDataStruct (41) z_arch (26) z_arch_job (27) z_automateTest (17) z_autoTrad` (19) z_bestPractice (39) z_bold (83) z_bondMath (35) z_book (18) z_boost (19) z_byRef^Val (32) z_c#GUI (43) z_c#misc (80) z_cast/convert (28) z_container (67) z_cStr/arr (39) z_Favorite* (8) z_FIX (15) z_forex (48) z_fwd_Deal (18) z_gz=job (33) z_gzBig20 (13) z_gzMgr (13) z_gzPain (20) z_gzThreat (19) z_hib (19) z_IDE (52) z_ikm (5) z_IR_misc (36) z_IRS (26) z_javaWeb (28) z_jdbc (10) z_jobFinTech (46) z_jobHunt (20) z_jobRealXp (10) z_jobStrength (15) z_jobUS^asia (27) z_letter (42) z_linq (10) z_memberHid` (11) z_MOM (54) z_nestedClass (5) z_oq (24) z_PCP (12) z_pearl (1) z_php (20) z_prodSupport (7) z_py (31) z_quant (14) z_regex (8) z_rv (38) z_skillist (48) z_slic`Problem (6) z_SOA (14) z_spring (25) z_src_code (8) z_swingMisc (50) z_swingTable (26) z_unpublish (2) z_VBA/Excel (8) z_windoz (17) z_wpfCommand (9)

About Me

New York (Time Square), NY, United States
http://www.linkedin.com/in/tanbin