All ITKnowledge Source Code Search Tips Advanced Search |

| Mastering Java Threads by Marc Adler and David Herst DDC Publishing, Inc. ISBN: 1562438425 Pub Date: 05/01/99

Search this book:

Previous | Table of Contents | Next

Daemon

isDaemon() | Returns the daemon flag of the Thread.
setDaemon(boolean) | Marks this Thread as a 1) daemon Thread or 2) user Thread.

Synchronization Methods

join(long) | Waits for this Thread to die.
join(long, int) | Waits for the Thread to die, with more precise time.
join() | Waits forever for this Thread to die.

Other Methods

Table 4-4: Additional thread methods
Method | Description
--- | ---
activeCount() | Returns the current number of active Threads in this Thread group.
checkAccess() | Checks whether the current Thread is allowed to modify this Thread.
currentThread() | Returns a reference to the currently executing Thread object.
dumpStack() | A debugging procedure to print a stack trace for the current Thread.
enumerate(Thread[]) | Copies, into the specified array, references to every active Thread in this Thread’s group.
interrupt() | Send an interrupt to a Thread.
interrupted() | Asks if you have been interrupted.
isInterrupted() | Asks if another Thread has been interrupted.

### Behind the Scenes: The Thread Class in C

As a Web programmer, you should be interested in learning how threads are actually implemented in the Java VM. Experimentation by programmers has revealed much. The Sun JDK also contains some files which provide information regarding the underlying implementation of Java.

Below is the underlying C structure that corresponds to the Thread class. This can be found in: THREADS/INCLUDE/JAVA_LANG_THREAD.H.

    typedef struct Classjava_lang_Thread

    {

        struct HArrayOfChar *name;

        long priority;

        struct Hjava_lang_Thread *threadQ;

        long PrivateInfo;

        long eetop;

        /*boolean*/ long single_step;

        /*boolean*/ long daemon;

        /*boolean*/ long stillborn;

        struct Hjava_lang_Runnable *target;

        struct Hjava_lang_ThreadGroup *group;

        long initial_stack_memory;

    } Classjava_lang_Thread;

Note the following regarding the internal representation of Threads within the Java VM:

  • Each thread has a name and a priority: there appears to be a pointer to a queue; this is possibly the queue on which Java keeps all Not Runnable threads. The variable eetop may be a pointer to the top of this thread’s stack area and the size of the stack space.
  • There are some boolean variables which relate to the thread’s state: there is a pointer to the thread’s Runnable object (some Thread constructors take a Runnable as an argument). Also, there is a pointer to the ThreadGroup to which the thread belongs.
  • The mystery is the PrivateInfo field: this may be used by each vendor that implements a Java VM and may contain operating-system dependent information (for example, it may contain the handle of an actual Win32 thread object).

Lab 2: Displaying Stock Information

Remember the stock files you generated in the first Lab exercise?

In this Lab, you will read them back in and display them in n different listboxes. Perform the following steps:

  1. Avoid hard-coding constant array sizes in this application. Therefore, one task that you must perform is to determine how many STK files are available in Java. (Hint: examine the Java File class).
  2. For each stock file, create a new Stock object that contains information regarding that stock. Read in the price information using one thread per stock file.
  3. Dynamically create a listbox for each stock file and fill each listbox with the prices for that stock. Use one thread per listbox into which you are going to insert data.
  4. Make this application an Applet. It has a user interface component (the three listboxes), and uses the Java AWT classes. Thus, a console application will not work here; you need to make this into an Applet.

Previous | Table of Contents | Next

Copyright © DDC Publishing

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.