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

Priorities

Table 8-3: Priorities
Priority | Description
--- | ---
getMaxPriority() | Gets the maximum priority of the group.
setMaxPriority(int) | Sets the maximum priority of the group.

Daemon Methods

Table 8-4: Daemon methods
Method | Description
--- | ---
isDaemon() | Returns the daemon flag of the Thread group.
setDaemon(boolean) | Changes the daemon status of this group.

Other Methods

Table 8-5: Other methods
Method | Description
--- | ---
checkAccesso() | Checks to see if the current Thread is allowed to modify this group.
getName() | Gets the name of this Thread group.
getParent() | Gets the parent of this Thread group.
parentOf(Thread Group) | Checks to see if this Thread group is a parent of or is equal to another Thread group.
toString() | Returns a String representation of the Thread group.
uncaughtException(Thread, Throwable) | Called when a thread in this group exists because of an uncaught exception.

#### Operating on an Entire Group of Threads

The following four methods operate on all of the threads in a specified ThreadGroup:

  • destroy()
  • resume()
  • stop()
  • suspend()

### Limiting Priorities

You can limit the priority value of any thread that is newly inserted into a ThreadGroup. The setMaxPriority() method will set a limit of the ThreadGroup as a whole.

For example, note the following code:

    ThreadGroup m_backgroundThreadGroup = new
          ThreadGroup(“BackgroundThreads”);
    Thread m_bgThread1 = new Thread(m_backgroundThreadGroup, this);
    m_backgroundThreadGroup.setMaxPriority(Thread.MIN_PRIORITY+2);
    Thread m_bgThread2 = new Thread(mPbackgroundThreadGroup, this);

#### Changing Priorities

The threads m_bgThread1 and m_bgThread2 are usually created with the default priority of NORMAL_PRIORITY, which is equal to the number 5. However, before the thread m_bgThread2 was created, the maximum priority of the ThreadGroup was set to MIN_PRIORITY+2, which is equal to the number 3. Thus, in this particular case, when m_bgThread2 was created, it actually received a priority value of 3.

Threads that are created before the call to setMaxPriority() will retain their old priority. Thus, in the case of the thread m_bgThread1, it still retains the priority value of 5 even though the max priority of the ThreadGroup was changed to 3.

#### Priorities Characteristics

Note the following characteristics of Java priorities:

  • setMaxPriority() will change the max priority value of all ThreadGroups which are descendants of a ThreadGroup.
  • If you attempt to set the priority of a thread higher than the priority of the ThreadGroup to which it belongs, a SecurityException will be thrown.
  • Once the maximum priority of a ThreadGroup has been lowered, it cannot be raised again.
  • The maximum priority of the system ThreadGroup is MAX-PRIORITY (10). The maximum priority of the applet ThreadGroup is 6.

| The setDaemon() function will change the daemon status of the ThreadGroup. It will not alter the daemon status of any individual thread within the group. If you make the group a daemon group, it will be destroyed automatically when all of the threads it contains are terminated.

### Java Security & checkAccess[] Method

Most users of the Internet—and Java applets in particular—are concerned about security. You would not want a Java applet from one Internet server illegally or maliciously modifying the ThreadGroups or threads of an applet on another Internet server. To this end, there exists some access checking regarding threads and ThreadGroups.

#### checkAccess[?] Method

The Thread and ThreadGroup classes both have a method called checkAccesso(). They both call the Security Manager’s checkAccess() method. The Security Manager checks to see if the threads are allowed access to certain operations. If they are, the Security Manager simply returns. If they are not, the Security Manager throws a SecurityException.

  • void checkAccess(Thread t): checks if the current thread is allowed to modify the state of thread.
  • void checkAccess(ThreadGroup g): checks if the current ThreadGroup is allowed to modify the state of ThreadGroup g.

Internally, most of the methods of class Thread and of class ThreadGroup call the Security Manager before performing the actual operation. For example, if you examine the implementation of the stop() method of the Thread class, you will find the following

    public void stop()
    {
       SecurityManager s = System.getSecurityManagero();
       If (s != null)
         s.checkAccess(this);
       stop0(new ThreadDeatho());
    }

#### All-of-none Security

The Java SecurityManager implements security on an “all-of-none” basis. There is no granularity to distinguish allowable operations. In other words, if the SecurityManager does not allow a thread to suspend another thread, then it also does not allow the same thread to set the other thread’s daemon status.

#### Calling ThreadGroup’s checkAccess[] Method

The following is a list of ThreadGroup methods that call the ThreadGroup’s checkAccesso() method:

  • ThreadGroup(ThreadGroup parent, String name)
  • setDaemon
  • setMaxPriority
  • stop
  • suspend
  • resume
  • destroy

#### Calling checkAccess[]

The following Thread methods call checkAccess():

  • constructors that specify a thread group
  • stop
  • suspend
  • resume
  • setPriority
  • setName
  • setDaemon

A stand-alone Java app has no security manager, so threads can modify and inspect any other thread.

| Within an Applet, a thread can manipulate another thread only if both threads are members of the Applet’s ThreadGroup. A thread cannot manipulate a thread that is in another 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.