这是<<JAVA编程思想>>里的一个例子,程序大意就是演示一下同步锁,不知为什么我这儿锁不住,哪位帮忙看看啊import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Sharing3 extends JApplet{
private static int accessCount=0;
private static JTextField aCount=new JTextField(7);
public static void incrementAccess(){
accessCount++;
aCount.setText(Integer.toString(accessCount));
}
private JButton
start=new JButton("Start"),
watcher=new JButton("Watch");
private boolean isApplet=true;
private int numCounters=12;
private int numWatchers=15;
private TwoCounter[] s;
class TwoCounter extends Thread{
private boolean started=false;
private JTextField
t1=new JTextField(5),
t2=new JTextField(5);
private JLabel l=new JLabel("count1 == count2");
private int count1=0,count2=0;
// Add the display components as a panel:
public TwoCounter(){
JPanel p=new JPanel();
p.add(t1);
p.add(t2);
p.add(l);
getContentPane().add(p);
}
public void start(){
if(!started)
{
started=true;
super.start();
}
}
public void run(){
while(true){
synchronized(this){
count1++;
count2++;
} t1.setText(Integer.toString(count1));
t2.setText(Integer.toString(count2)); try{
sleep(500);
}
catch(InterruptedException e){
System.err.println("Interrupted");
}
}
}
public synchronized void synchTest(){
//public void synchTest(){
Sharing3.incrementAccess();
if(count1!=count2);
l.setText("Unsynched");
}
}
class Watcher extends Thread{
public Watcher(){
start();
}
public void run(){
while(true){
for(int i=0;i<s.length;i++)
s[i].synchTest();
try{
sleep(500);
}
catch(InterruptedException e){
System.err.println("Interrupted");
}
}
}
}
class StartL implements ActionListener{
public void actionPerformed(ActionEvent e){
for(int i=0;i<s.length;i++)
s[i].start();
}
}
class WatcherL implements ActionListener{
public void actionPerformed(ActionEvent e){
for(int i=0;i<numWatchers;i++)
new Watcher();
}
}
public void init(){
if(isApplet){
String counters=getParameter("size");
if(counters!=null)
numCounters=Integer.parseInt(counters);
String watchers=getParameter("watchers");
if(watchers!=null)
numWatchers=Integer.parseInt(watchers);
}
s=new TwoCounter[numCounters];
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
for(int i=0;i<s.length;i++)
s[i]=new TwoCounter();
JPanel p=new JPanel();
start.addActionListener(new StartL());
p.add(start);
watcher.addActionListener(new WatcherL());
p.add(watcher);
p.add(new JLabel("Access Count"));
p.add(aCount);
cp.add(p);
}
public static void main(String[] args){
Sharing3 applet=new Sharing3();
// This isn't an applet,so set the flag and produce the parameter values from args:
applet.isApplet=false;
applet.numCounters=(args.length == 0 ? 12 : Integer.parseInt(args[0]));
applet.numWatchers=(args.length<2 ? 15 : Integer.parseInt(args[1]));
}
}
import java.awt.*;
import java.awt.event.*;public class Sharing3 extends JApplet{
private static int accessCount=0;
private static JTextField aCount=new JTextField(7);
public static void incrementAccess(){
accessCount++;
aCount.setText(Integer.toString(accessCount));
}
private JButton
start=new JButton("Start"),
watcher=new JButton("Watch");
private boolean isApplet=true;
private int numCounters=12;
private int numWatchers=15;
private TwoCounter[] s;
class TwoCounter extends Thread{
private boolean started=false;
private JTextField
t1=new JTextField(5),
t2=new JTextField(5);
private JLabel l=new JLabel("count1 == count2");
private int count1=0,count2=0;
// Add the display components as a panel:
public TwoCounter(){
JPanel p=new JPanel();
p.add(t1);
p.add(t2);
p.add(l);
getContentPane().add(p);
}
public void start(){
if(!started)
{
started=true;
super.start();
}
}
public void run(){
while(true){
synchronized(this){
count1++;
count2++;
} t1.setText(Integer.toString(count1));
t2.setText(Integer.toString(count2)); try{
sleep(500);
}
catch(InterruptedException e){
System.err.println("Interrupted");
}
}
}
public synchronized void synchTest(){
//public void synchTest(){
Sharing3.incrementAccess();
if(count1!=count2);
l.setText("Unsynched");
}
}
class Watcher extends Thread{
public Watcher(){
start();
}
public void run(){
while(true){
for(int i=0;i<s.length;i++)
s[i].synchTest();
try{
sleep(500);
}
catch(InterruptedException e){
System.err.println("Interrupted");
}
}
}
}
class StartL implements ActionListener{
public void actionPerformed(ActionEvent e){
for(int i=0;i<s.length;i++)
s[i].start();
}
}
class WatcherL implements ActionListener{
public void actionPerformed(ActionEvent e){
for(int i=0;i<numWatchers;i++)
new Watcher();
}
}
public void init(){
if(isApplet){
String counters=getParameter("size");
if(counters!=null)
numCounters=Integer.parseInt(counters);
String watchers=getParameter("watchers");
if(watchers!=null)
numWatchers=Integer.parseInt(watchers);
}
s=new TwoCounter[numCounters];
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
for(int i=0;i<s.length;i++)
s[i]=new TwoCounter();
JPanel p=new JPanel();
start.addActionListener(new StartL());
p.add(start);
watcher.addActionListener(new WatcherL());
p.add(watcher);
p.add(new JLabel("Access Count"));
p.add(aCount);
cp.add(p);
}
public static void main(String[] args){
Sharing3 applet=new Sharing3();
// This isn't an applet,so set the flag and produce the parameter values from args:
applet.isApplet=false;
applet.numCounters=(args.length == 0 ? 12 : Integer.parseInt(args[0]));
applet.numWatchers=(args.length<2 ? 15 : Integer.parseInt(args[1]));
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货