Java Reference
In-Depth Information
Running in thread Thread-1
WorkerThread1: Done
The code for
NonBlockingFrame
is listed below and the resulting display is shown
in Figure 20.1.
File:
its/Blocking/NonBlockingFrame.java
1.
package
its.Blocking;
2.
3.
import
its.SimpleFrame.SimpleFrame;
4.
import
java.awt.event.ActionEvent;
5.
import
java.awt.event.ActionListener;
6.
import
javax.swing.JLabel;
7.
import
javax.swing.JMenu;
8.
import
javax.swing.JMenuBar;
9.
import
javax.swing.JMenuItem;
10.
11.
public class
NonBlockingFrame
extends
SimpleFrame
12.
{
13.
14.
private
JLabel display;
15.
private int
nummer;
16.
17.
public
NonBlockingFrame(){
18.
nummer = 0;
19.
this
.setTitle("Non blocking frame");
20.
display =
new
JLabel("Nothing happened");
21.
this
.getContentPane().add(display);
22.
23.
JMenuBar menuBar
=
new
JMenuBar();
24.
this
.setJMenuBar(menuBar);
25.
JMenu
menu
=
new
JMenu("Menu1");
26.
menuBar.add(menu);
27.
JMenuItem startItem =
new
JMenuItem("Start");
28.
menu.add(startItem);
29.
MenuListener mListener =
new
MenuListener();
30.
startItem.addActionListener(mListener);
31.
}
32.
33.
private void
lengthyWork(){
34.
//
This is the lengthy work
35.
for
(
int
i=0;i<10; i++){
36.
System.out.println("Working "+i);
37.
System.out.println("Running in thread "+
Thread.currentThread().getName());
Search WWH ::
Custom Search