public class TestThread extends java.lang.Object implements java.lang.Runnable
Modifier and Type | Method and Description |
---|---|
boolean |
isThreadFinished()
Returns true if the class private variable
threadFinished is set. |
void |
otherThreadRoutine()
This routine is called from outside in another thread.
|
void |
run()
This is the thread main-routine complying the Java rules.
|
void |
start()
Facade routine to start the Thread.
|
public void run()
theThread
,
is called with ,,start(),,-method.
implements Runnable.run()
@ java2c=stackInstance.
is written thereby. Large-size instances need
an adequate stack size. The Java-code for this code-snippet is:
...* @java2c=stackInstance. * / TestThreadLocalData threadLocalData = new TestThreadLocalData();The generated C-code is:
TestThread_Test__TestThreadLocalData_s threadLocalData; ... init_ObjectJc(&(threadLocalData.base.object), sizeof(threadLocalData), 0); ctorO_TestThread_Test__TestThreadLocalData(&(threadLocalData.base.object), _thCxt);The thread contains a
for
-loop to force a determined call of testSynchronized
,
and a call of notify(...)
to test the wait/notify-concept with a third thread.
Than a sleep is called to delay the execution of the thread with deterministic milliseconds-time.
The Java-form is:
for(int liveCt = 0; liveCt <200; liveCt++){ testSynchronized(threadLocalData); theNotifyingData.notify(testCt1); try{ Thread.sleep(10); } catch(InterruptedException exc){ testCtInterrupted +=1; } }//forThe catch clause is processed if the thread was woken up abnormal. The coding of this catch is prescribed in Java. In C it is mapped too, but it isn't used yet.
for(liveCt = 0; liveCt < 200; liveCt++) { testSynchronized_TestThread_Test(ythis, & (threadLocalData), _thCxt); theNotifyingDataMtbl.mtbl->notify( (theNotifyingDataMtbl.ref), ythis->testCt1, _thCxt); TRY { sleep_ThreadJc(10, _thCxt); }_TRY CATCH(InterruptedException, exc) { ythis->testCtInterrupted += 1; } END_TRY }The call of
theNotifyingDataMtbl.mtbl->notify(...)
is executed dynamically,
because the destination instance can be an derived class of the reference type
in an enhancement of the example. To prevent effort to get the method table reference,
the Java-code contains a stack-local reference at top of the routine,
see Docu.D_SuperClassesAndInterfaces.D6_callingOverrideableMethods()
.
/**Use local variable to enforce only one preparation of the method table for dynamic call:
run
in interface java.lang.Runnable
public void start()
/ **@java2c=stackSize(TestThreadLocalData+2000). * / theThread.start();The stackSize is necessary for the C-implementation. Because an instance of
TestThreadLocalData
is created in the run()
-Method, its size should be regarded.
The rest, 2000 Bytes, is a proper value for typical stack usages.
start_ThreadJc(& (ythis->theThread) , sizeof(TestThread_Test__TestThreadLocalData_s)+2000, _thCxt);The stackSize-annotation is translated to the stackSize-parameter-value for the
start_ThreadJc
-routine.
The really size should be tested at C-level-debugging.public void otherThreadRoutine()
synchronized
adequate to run()
.
This routine works with 500 loops in a time of 10 ms. At its end #shouldRun
is set to false
,
which causes an aborting of the run()
-routine and therefore the finishing of the thread.public final boolean isThreadFinished()
threadFinished
is set. That occurs at last action
of the run()
-routine of the thread.