File: thrdtest.java

package info (click to toggle)
jswat 1.7-2
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k
  • size: 5,656 kB
  • ctags: 3,210
  • sloc: java: 24,683; xml: 130; makefile: 59; sh: 21
file content (109 lines) | stat: -rw-r--r-- 3,196 bytes parent folder | download
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
// A threading test for JSwat.
// $Author: nfiedler $ $Date: 2002-10-13 00:03:33 -0700 (Sun, 13 Oct 2002) $ $Rev: 604 $

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, Runnable {

    /**
     * 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("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.show();
    }

    /**
     * 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(this);
                th.start();
            }
        } else if (cmd.equals("makeThrdA")) {
            Thread th = new Thread(this, "ThrdA");
            th.start();
        } else if (cmd.equals("makeThrdB")) {
            Thread th = new Thread(this, "ThrdB");
            th.start();
//         } else if (cmd.equals("stopTest")) {
//             Thread th = new Thread(this, "Victim");
//             th.start();
//             th.stop();
        }
    }

    /**
     * Sleep for somewhere between 1 and 20 seconds.
     */
    public void run() {
        // Pick random time from 1 to 20 seconds and sleep that long.
        long delay = (long) (Math.random() * 19000) + 1000;
        try {
            Thread.sleep(delay);
        } catch (InterruptedException ie) { }
    }

    /**
     * Main method.
     *
     * @param  args  command-line arguments.
     */
    public static void main(String[] args) {
        new thrdtest();
    }
}