Pat Chan noted that Thread.init doesn't always respect the max priority set
for the thread group of the thread being created. The fix is trivial. He wrote:
If you create a thread group and restrict its priority to minimum,
the priority of new threads are not set to the group's maximum priority.
However, if you ever call
setPriority()
on the thread, the thread's priority will indeed be set to minimum.
In Thread.init(), you need to call setPriority() rather than
setPriority0().
Here's a test program. [Tim: used to print 5 for all threads, and after fix
should print 1's for loGroup and 5's for hiGroup...]
import java.awt.*;
class Main extends Frame {
Label loLabel = new Label();
Label hiLabel = new Label();
ThreadGroup hiGroup = new ThreadGroup("High");
ThreadGroup loGroup = new ThreadGroup("Low");
Main() {
super("suspend Example");
// Limit the priority in the lo priority thread group.
loGroup.setMaxPriority(Thread.MIN_PRIORITY);
// Add a few threads.
(new Worker(hiGroup, hiLabel)).start();
(new Worker(hiGroup, hiLabel)).start();
(new Worker(loGroup, loLabel)).start();
(new Worker(loGroup, loLabel)).start();
add("North", hiLabel);
add("South", loLabel);
add("Center", new Button("suspend"));
pack();
show();
}
public synchronized boolean handleEvent(Event evt) {
if ("suspend".equals(evt.arg)) {
hiGroup.suspend();
((Button)evt.target).setLabel("resume");
return true;
} else if ("resume".equals(evt.arg)) {
hiGroup.resume();
((Button)evt.target).setLabel("suspend");
return true;
}
return false;
}
static public void main(String[] args) {
new Main();
}
}
class Worker extends Thread {
Label label;
Worker(ThreadGroup group, Label label) {
super(group, "Worker");
this.label = label;
}
public void run() {
try {
for (int i=0; ; i++) {
if (i % 10 == 0) {
label.setText(""+i);
System.out.println(getPriority());
}
Thread.sleep(16);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
for the thread group of the thread being created. The fix is trivial. He wrote:
If you create a thread group and restrict its priority to minimum,
the priority of new threads are not set to the group's maximum priority.
However, if you ever call
setPriority()
on the thread, the thread's priority will indeed be set to minimum.
In Thread.init(), you need to call setPriority() rather than
setPriority0().
Here's a test program. [Tim: used to print 5 for all threads, and after fix
should print 1's for loGroup and 5's for hiGroup...]
import java.awt.*;
class Main extends Frame {
Label loLabel = new Label();
Label hiLabel = new Label();
ThreadGroup hiGroup = new ThreadGroup("High");
ThreadGroup loGroup = new ThreadGroup("Low");
Main() {
super("suspend Example");
// Limit the priority in the lo priority thread group.
loGroup.setMaxPriority(Thread.MIN_PRIORITY);
// Add a few threads.
(new Worker(hiGroup, hiLabel)).start();
(new Worker(hiGroup, hiLabel)).start();
(new Worker(loGroup, loLabel)).start();
(new Worker(loGroup, loLabel)).start();
add("North", hiLabel);
add("South", loLabel);
add("Center", new Button("suspend"));
pack();
show();
}
public synchronized boolean handleEvent(Event evt) {
if ("suspend".equals(evt.arg)) {
hiGroup.suspend();
((Button)evt.target).setLabel("resume");
return true;
} else if ("resume".equals(evt.arg)) {
hiGroup.resume();
((Button)evt.target).setLabel("suspend");
return true;
}
return false;
}
static public void main(String[] args) {
new Main();
}
}
class Worker extends Thread {
Label label;
Worker(ThreadGroup group, Label label) {
super(group, "Worker");
this.label = label;
}
public void run() {
try {
for (int i=0; ; i++) {
if (i % 10 == 0) {
label.setText(""+i);
System.out.println(getPriority());
}
Thread.sleep(16);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}