1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// A threading test for JSwat.
// $Id: thrdtest.java 1113 2004-01-25 22:25:32Z nfiedler $
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* Thread test bed.
*
* @author Nathan Fiedler
*/
public class thrdtest implements ActionListener {
/**
* Create the thread test window.
*/
public thrdtest() {
JFrame frame = new JFrame("thread tester");
Container pane = frame.getContentPane();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
pane.setLayout(gbl);
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
JButton button = new JButton("Start 10 threads");
button.setActionCommand("makeTen");
button.addActionListener(this);
gbl.setConstraints(button, gbc);
pane.add(button);
button = new JButton("Start 'ThrdA' thread");
button.setActionCommand("makeThrdA");
button.addActionListener(this);
gbl.setConstraints(button, gbc);
pane.add(button);
button = new JButton("Start 'ThrdB' thread");
button.setActionCommand("makeThrdB");
button.addActionListener(this);
gbl.setConstraints(button, gbc);
pane.add(button);
button = new JButton("Start thread in group");
button.setActionCommand("makeGrpThrd");
button.addActionListener(this);
gbl.setConstraints(button, gbc);
pane.add(button);
button = new JButton("Start no-name thread");
button.setActionCommand("makeNomanThrd");
button.addActionListener(this);
gbl.setConstraints(button, gbc);
pane.add(button);
button = new JButton("Start many threads");
button.setActionCommand("makeManyThrds");
button.addActionListener(this);
gbl.setConstraints(button, gbc);
pane.add(button);
// button = new JButton("Stop test");
// button.setActionCommand("stopTest");
// button.addActionListener(this);
// gbl.setConstraints(button, gbc);
// pane.add(button);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setLocation(100, 100);
frame.setVisible(true);
}
/**
* Invoked when an action occurs.
*
* @param e action event.
*/
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("makeTen")) {
// Create 10 threads to run for a while.
for (int i = 0; i < 10; i++) {
Thread th = new Thread(new Sleeper());
th.start();
}
} else if (cmd.equals("makeThrdA")) {
new Thread(new Sleeper(), "ThrdA").start();
} else if (cmd.equals("makeThrdB")) {
new Thread(new Sleeper(), "ThrdB").start();
} else if (cmd.equals("makeNomanThrd")) {
new Thread(new Sleeper(), "").start();
} else if (cmd.equals("makeManyThrds")) {
manyThreads();
} else if (cmd.equals("makeGrpThrd")) {
new Thread(new ThreadGroup("group-1"), new Sleeper(),
"thread-1").start();
// } else if (cmd.equals("stopTest")) {
// Thread th = new Thread(this, "Victim");
// th.start();
// th.stop();
}
}
protected void manyThreads() {
// Spin this off to another thread.
Thread th = new Thread(new Runnable() {
public void run() {
// Spawn a bunch of long-lived threads.
for (int i = 0; i < 200; i++) {
new Thread(new Sleeper(180000, 120000)).start();
}
// Then create short-lived threads frequently.
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
// ignored
}
new Thread(new Sleeper(500, 500)).start();
}
}
});
th.start();
}
public static void main(String[] args) {
new thrdtest();
}
/**
* Sleeps for some amount of time.
*/
protected class Sleeper implements Runnable {
private int base;
private int duration;
public Sleeper() {
// Default to 1 to 20 seconds.
base = 1000;
duration = 19000;
}
public Sleeper(int base, int duration) {
this.base = base;
this.duration = duration;
}
public void run() {
// Pick random time and sleep that long.
long delay = (long) (Math.random() * duration) + base;
try {
Thread.sleep(delay);
} catch (InterruptedException ie) {
// ignored
}
}
}
}
|