环境jb2006。package jiang;import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.Rectangle;
import javax.swing.JTable;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class jiang extends Applet implements Runnable{
protected static String[] stringArray=new String[10];
boolean isStandalone = false;
BorderLayout borderLayout1 = new BorderLayout();
JButton startjButton = new JButton();
JButton endjButton = new JButton();
JTable jTable1 = new JTable(10,1);
DatabaseThread databaseThread=new DatabaseThread();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public jiang() {
} //Initialize the applet
public void init() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
int i;
this.setLayout(null);
startjButton.setBounds(new Rectangle(62, 250, 87, 23));
startjButton.setActionCommand("startjButton");
startjButton.setText("开始");
startjButton.addActionListener(new jiang_startjButton_actionAdapter(this));
endjButton.setBounds(new Rectangle(258, 250, 87, 23));
endjButton.setActionCommand("endjButton");
endjButton.setText("结束");
endjButton.addActionListener(new jiang_endjButton_actionAdapter(this));
jTable1.setCellSelectionEnabled(false);
jTable1.setBounds(new Rectangle(47, 42, 312, 160));
this.add(jTable1);
this.add(startjButton);
this.add(endjButton);
for(i=0;i<10;i++)
{
stringArray[i]=new String("");
}
}
public void paint(Graphics g)
{
int i;
super.paint(g);
for(i=1;i<10;i++)
{
jTable1.setValueAt(stringArray[i],i,0);
}
}
public void run ()
{
repaint();
try{
wait();
}catch(Exception e)
{
}
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
} //Get parameter info
public String[][] getParameterInfo() {
return null;
} public void endjButton_actionPerformed(ActionEvent e) {
databaseThread.continuerun=false;
} public void startjButton_actionPerformed(ActionEvent e) {
databaseThread.continuerun=true;
try{
databaseThread.start();
}catch(IllegalThreadStateException iTSE){
JOptionPane.showMessageDialog(null,"Already started");
}
this.start();
}
}
class jiang_startjButton_actionAdapter implements ActionListener {
private jiang adaptee;
jiang_startjButton_actionAdapter(jiang adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.startjButton_actionPerformed(e);
}
}
class jiang_endjButton_actionAdapter implements ActionListener {
private jiang adaptee;
jiang_endjButton_actionAdapter(jiang adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.endjButton_actionPerformed(e);
}
}//一个从数据库读身份证号的类,作为测试,用随机数代替。class DatabaseThread extends Thread{
protected boolean continuerun=true;
public DatabaseThread()
{ } public void run()
{
int i;
Long ID;
while (continuerun)
{
for(i=0;i<9;i++)
{
jiang.stringArray[i]=jiang.stringArray[i+1];
}
ID=new Long((int)(Math.random()*9999999999999l));
jiang.stringArray[i]=ID.toString();
notify();
try{
sleep(500);
}catch(Exception e)
{}
}
} protected void finalize()
{ }
}
运行时,一点开始按钮,jTable1的最后一行会出现一个结果,然后就会提示以下错误:Exception in thread "Thread-2" java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at jiang.DatabaseThread.run(jiang.java:170)
所提示的行号为DatabaseThread类的run方法的notify()调用处。
请问该问题该如何解决?
import java.awt.event.*;
import java.applet.*;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.Rectangle;
import javax.swing.JTable;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class jiang extends Applet implements Runnable{
protected static String[] stringArray=new String[10];
boolean isStandalone = false;
BorderLayout borderLayout1 = new BorderLayout();
JButton startjButton = new JButton();
JButton endjButton = new JButton();
JTable jTable1 = new JTable(10,1);
DatabaseThread databaseThread=new DatabaseThread();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public jiang() {
} //Initialize the applet
public void init() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
int i;
this.setLayout(null);
startjButton.setBounds(new Rectangle(62, 250, 87, 23));
startjButton.setActionCommand("startjButton");
startjButton.setText("开始");
startjButton.addActionListener(new jiang_startjButton_actionAdapter(this));
endjButton.setBounds(new Rectangle(258, 250, 87, 23));
endjButton.setActionCommand("endjButton");
endjButton.setText("结束");
endjButton.addActionListener(new jiang_endjButton_actionAdapter(this));
jTable1.setCellSelectionEnabled(false);
jTable1.setBounds(new Rectangle(47, 42, 312, 160));
this.add(jTable1);
this.add(startjButton);
this.add(endjButton);
for(i=0;i<10;i++)
{
stringArray[i]=new String("");
}
}
public void paint(Graphics g)
{
int i;
super.paint(g);
for(i=1;i<10;i++)
{
jTable1.setValueAt(stringArray[i],i,0);
}
}
public void run ()
{
repaint();
try{
wait();
}catch(Exception e)
{
}
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
} //Get parameter info
public String[][] getParameterInfo() {
return null;
} public void endjButton_actionPerformed(ActionEvent e) {
databaseThread.continuerun=false;
} public void startjButton_actionPerformed(ActionEvent e) {
databaseThread.continuerun=true;
try{
databaseThread.start();
}catch(IllegalThreadStateException iTSE){
JOptionPane.showMessageDialog(null,"Already started");
}
this.start();
}
}
class jiang_startjButton_actionAdapter implements ActionListener {
private jiang adaptee;
jiang_startjButton_actionAdapter(jiang adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.startjButton_actionPerformed(e);
}
}
class jiang_endjButton_actionAdapter implements ActionListener {
private jiang adaptee;
jiang_endjButton_actionAdapter(jiang adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.endjButton_actionPerformed(e);
}
}//一个从数据库读身份证号的类,作为测试,用随机数代替。class DatabaseThread extends Thread{
protected boolean continuerun=true;
public DatabaseThread()
{ } public void run()
{
int i;
Long ID;
while (continuerun)
{
for(i=0;i<9;i++)
{
jiang.stringArray[i]=jiang.stringArray[i+1];
}
ID=new Long((int)(Math.random()*9999999999999l));
jiang.stringArray[i]=ID.toString();
notify();
try{
sleep(500);
}catch(Exception e)
{}
}
} protected void finalize()
{ }
}
运行时,一点开始按钮,jTable1的最后一行会出现一个结果,然后就会提示以下错误:Exception in thread "Thread-2" java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at jiang.DatabaseThread.run(jiang.java:170)
所提示的行号为DatabaseThread类的run方法的notify()调用处。
请问该问题该如何解决?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货