06.08.2013 Views

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.2. Resources and mutual exclusion – Semaphores<br />

subclassing Thread or by implementing the Runnable interface which simply<br />

states the presence of the run method:<br />

public interface Runnable {<br />

void run();<br />

}<br />

When implementing Runnable you need to call Thread.currentThread() to get<br />

to the public non-static methods of class Thread, which in short includes the<br />

following:<br />

public class Thread implements Runnable {<br />

}<br />

static int MAX_PRIORITY; // Highest possible priority.<br />

static int MIN_PRIORITY; // Lowest possible priority.<br />

static int NORM_PRIORITY; // Default priority.<br />

Thread(); // Use run in subclass.<br />

Thread(Runnable target); // Use the run of ’target ’.<br />

void start(); // Create thread which calls run.<br />

void run() {}; // Work to be defined by subclass.<br />

static Thread currentThread ();// Get currently executing thread.<br />

void setPriority(int pri); // Change the priority to ’pri ’.<br />

int getPriority (); // Returns this thread’s priority.<br />

static void sleep(long t); // Suspend execution at least ’t’ ms<br />

static void yield(); // Reschedule to let others run.<br />

void interrupt(); // Set interrupt request flag.<br />

boolean isInterrupted (); // Check interrupt flag of thread obj.<br />

static boolean interrupted (); // Check and clear for current thread.<br />

boolean isAlive(); // True if started but not dead.<br />

void join(); // Waits for this thread to die.<br />

void join(long t); // Try to join , but only for ’t’ ms.<br />

Note that the Thread class does not relate to real time. Instead, the realtime<br />

clock is, as mentioned, obtained by calling System.currentTimeMillis(); which<br />

returns the value of the real-time clock as a long expressed in milliseconds.<br />

See the JDK class documentation for further information.<br />

3.2 Resources and mutual exclusion – Semaphores<br />

Semaphores are of interest as a basic mechanism that is available in one form<br />

or another in all (?) operating systems and real-time kernels. In some industrial<br />

systems, the only available mechanism is semaphores, on which other<br />

concepts could be built. Also historically, the semaphore was the first (introduced<br />

by Dijkstra in 1968) principle for efficiently handling mutual exclusion<br />

and synchronization, solving problems such as the bank account transactions<br />

described earlier.<br />

A Semaphore is a mechanism that can be used for implementation of the<br />

fundamental abstractions shown in the previous chapter. Such a mechanism<br />

55

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!