这是<<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]));

}
}

解决方案 »

  1.   

    哪里锁不住了,java编程思想里有这个例子?
      

  2.   

    是这样的,这个程序如count1和count2不相等的时候,就会调用l.setText("Unsynched");把label设置成unsynched,因为我用了同步锁,所以点击watch按钮的时候只会出现count1==count2,不会出现Unsynched,但结果却不是这样子,出现了Unsynched,就说明当count1++的时候,锁已经被释放了,这时候执行Watcher线程,所以count1!=count2,所以结果是不对的,但不应该是这样的啊