org.vishia.java2C.test
Class TestWaitNotify

java.lang.Object
  extended by java.lang.Thread
      extended by org.vishia.java2C.test.TestWaitNotify
All Implemented Interfaces:
java.lang.Runnable

public class TestWaitNotify
extends java.lang.Thread

This class demonstrates and tests the usage of threads with wait and notify. It implements the run()-Method of the Thread defined in the interface Runnable, which is inherited from the super class Thread. The class based on Thread, therefore no other instance is necessary to organize the thread.

The threads private data which should be able to access only by the current thread are stored in an extra class, here defined as static inner class.


Nested Class Summary
private static class TestWaitNotify.TestThreadLocalData
          This class is defined only to use in the threads context.
static class TestWaitNotify.WaitNotifyData
          This class is visible from outside, it is used from the notifying thread and from this thread, which waits for data.
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
(package private)  boolean shouldRun
          This is a central thread-control variable.
(package private)  TestWaitNotify.WaitNotifyData theAwaitingData
          Aggregation to data to check wait/notify.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TestWaitNotify(TestWaitNotify.WaitNotifyData theAwaitingDataP)
          Constructor.
 
Method Summary
 void run()
          This is the thread main-routine complying the Java rules.
 void start()
          This routine overrides Thread.start(), it's a facade.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

shouldRun

boolean shouldRun
This is a central thread-control variable. If it is set to false, the thread should finish.


theAwaitingData

final TestWaitNotify.WaitNotifyData theAwaitingData
Aggregation to data to check wait/notify.

Constructor Detail

TestWaitNotify

TestWaitNotify(TestWaitNotify.WaitNotifyData theAwaitingDataP)
Constructor.

Parameters:
theAwaitingData - for aggregation
Method Detail

start

public void start()
This routine overrides Thread.start(), it's a facade. It calls Thread.start() using
    /**@java2c=stackSize(TestThreadLocalData+500). * /
    super.start();
 
It is a facade, containing the stacksize annotation regarded in Java2C-translation. The produced C-Code is:
    start_ThreadJc(ythis, sizeof(TestWaitNotify_Test__TestThreadLocalData_s)+500, _thCxt);
 

Overrides:
start in class java.lang.Thread
See Also:
java.lang.Thread#start()}

run

public void run()
This is the thread main-routine complying the Java rules. The routine is started if the ,,start(),,-method of this instance is called.

This routine creates an instances TestWaitNotify.TestThreadLocalData, which are accessed by this thread only. In Java the instances are referenced, but the reference is only known in stack context, provided to called routines via parameter. In C the instances are allocated in the stack because a @ 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 while-loop with test of shouldRun and a sleep in the Java-form:
    while(shouldRun){
      threadLocalData.awaitData();
    }//while
 
In C it is mapped too, but it isn't used yet.

The C-code of this snippet is:
    while(ythis->shouldRun) {
      awaitData_TestWaitNotify_Test__TestThreadLocalData_F(& (threadLocalData), _thCxt);
    }
 

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread