📄 suin.java
字号:
21Concurrency
Exercise 12:(3)Repair AtomicityTest.java using the synchronized keyword.Can you demonstrate that it is now correct?
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class AtomicityTest implements Runnable
{
private int i = 0;
public int getValue()
{
return i;
}
private synchronized void evenIncrement()
{
i++;
i++;
}
public static void main(String[] args)
{
ExecutorService exec = Executors.newCachedThreadPool();
AtomicityTest at = new AtomicityTest();
exec.execute(at);
while(true)
{
int val = at.getValue();
if(val % 2 != 0)
{
System.out.println(val);
System.exit(0);
}
}
}
public void run()
{
while(true)
evenIncrement();
}
}
13199881
22Graphical User Interfaces
A selection of Swing componets
Exercise 18:(4)Modify MessageBoxes.java so that it has an indidual ActionListener for each button (instead of matching the button text).
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import com.bruceeckel.swing.*;
class MessageBoxes extends JApplet {
JButton
b1 = new JButton("Alert"),
b2 = new JButton("Yes/No"),
b3 = new JButton("Color"),
b4 = new JButton("Input"),
b5 = new JButton("3 Vals");
JTextField txt = new JTextField(15);
public void init() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(b3);
cp.add(b4);
cp.add(b5);
cp.add(txt);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"There's a bug on you!", "Hey!",
JOptionPane.ERROR_MESSAGE);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(null,
"or no", "choose yes",
JOptionPane.YES_NO_OPTION);
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] options = { "Red", "Green" };
int sel = JOptionPane.showOptionDialog(
null, "Choose a Color!", "Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null,
options, options[0]);
if(sel != JOptionPane.CLOSED_OPTION)
txt.setText(
"Color Selected: " + options[sel]);
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String val = JOptionPane.showInputDialog(
"How many fingers do you see?");
txt.setText(val);
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] selections = {
"First", "Second", "Third" };
Object val = JOptionPane.showInputDialog(
null, "Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE,
null, selections, selections[0]);
if(val != null)
txt.setText(
val.toString());
}
});
}
}
public class E11_MessageAction {
public static void main(String args[]) {
Console.run(new MessageBoxes(), 200, 200);
}
}
22Graphical User Interfaces
Exercise 1:(1)Modify HelloSwing.java to prove to yourself that the application will not close without the call to setDefaultCloseOperation().
//:gui/HelloSwing.java
import javax.Swing.*;
public class HelloSwing{
pubic static void main(String[] args){
JFrame frame = new JFrame("Hello Swing");
frame.setSize(300,100);
frame.setVisible(ture);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -