一段程序里用到的同步的地方,不太明白

fxbird 2006-02-01 09:21:15
源程序:
package thread;
/**
* 这个表格模型显示了当前运行的所有线程的情况,如是否为守护线程,是否活着等等 ,每5秒钟刷新一下
*/

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class ThreadViewerTableModel extends AbstractTableModel{
private Object dataLook; //为了同步用的一个变量
private int rowCount;
private Object [][] cellData;//单元格的数据
private Object[][] pendingCellData; //我也不太明白 ,好像是临时保存一下最新数据,再赋值给cellData

private final int columnCount;
private final String [] columnName;
private final Class[] columnClass;//列的类型
private Thread internalThread; //内部线程,用来自动刷新的线程,调用查询当前所有线程并修改表格的数据
private volatile boolean noStopRequested; //是否停止,这里没有用到

public ThreadViewerTableModel() {
this.rowCount=0;
this.cellData=new Object[0][0];

String [] names={"优先级","存活","守护线程","中断了","线程组","线程名"};
columnName=names;


Class [] classes={Integer.class,Boolean.class,Boolean.class,Boolean.class,String.class,String.class};
this.columnClass=classes;

this.columnCount=columnName.length;

this.dataLook=new Object();

this.noStopRequested=true;
Runnable r=new Runnable(){
public void run() {
runWork();
}
};

this.internalThread=new Thread(r,"ThreadViewer");
this.internalThread.setPriority(Thread.MAX_PRIORITY-2);
this.internalThread.setDaemon(true);
this.internalThread.start();

}


public void runWork(){
Runnable transferPending=new Runnable(){
public void run() {
transferPendingCellData();// 将最新数据赋值给cellData
fireTableDataChanged();
}
};

while(this.noStopRequested){

createPendingCellData(); //求得最新数据
try {
SwingUtilities.invokeAndWait(transferPending);//更新表格的界面
Thread.sleep(5000);//定时5秒
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
Thread.currentThread().interrupt();
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
stopRequest();
}
}

}

/**
* 停止内部线程
*/
public void stopRequest(){
this.noStopRequested=false;
this.internalThread.interrupt();
}

public boolean isAlive(){
return this.internalThread.isAlive();
}

/**
* 生成最新的数据
*/
private void createPendingCellData(){
Thread[] thread=findAllThreads();
Object[][] cell=new Object[thread.length][columnCount];

for (int i = 0; i < thread.length; i++) {
Thread t=thread[i];
Object [] rowCell=cell[i];
rowCell[0]=new Integer(t.getPriority());
rowCell[1]=new Boolean(t.isAlive());
rowCell[2]=new Boolean(t.isDaemon());
rowCell[3]=new Boolean(t.isInterrupted());
rowCell[4]=new Boolean(t.getThreadGroup().getName());
rowCell[5]=t.getName();

}

//这里为什么要同步,岂非只有一个内部线程???????????
synchronized(this.dataLook){
this.pendingCellData=cell;

}

}

public void transferPendingCellData(){
//同样是只有一个内部线程,为什么要使用同步???????????
synchronized(dataLook){
cellData = pendingCellData;
rowCount=cellData.length;
}

}

public int getRowCount() {
return rowCount;
}

public Object getValueAt(int row,int col){
return cellData[row][col];

}

public int getColumnCount(){
return columnCount;
}

public Class getColumnClass(int columnIndex) {
return columnClass[columnIndex];
}

public String getColumnName(int column) {
return columnName[column];
}

public static Thread[] findAllThreads(){
ThreadGroup group=Thread.currentThread().getThreadGroup();
ThreadGroup topGroup=group;
while(group!=null){
topGroup=group;
group=group.getParent();
}

int estimateSize=topGroup.activeCount();
Thread[] slackList=new Thread[estimateSize];

int actualSize=topGroup.enumerate(slackList);

Thread[] list=new Thread[actualSize];
System.arraycopy(slackList,0,list,0,actualSize);

return list;
}
}

调用示例程序:
package thread;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2006-2-1
* Time: 20:01:13
* To change this template use File | Settings | File Templates.
*/
public class ThreadViewer extends JPanel{
private ThreadViewerTableModel tableModel;

public ThreadViewer() {
this.tableModel=new ThreadViewerTableModel();

JTable table=new JTable(tableModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

TableColumnModel colModel=table.getColumnModel();
int numColumns=colModel.getColumnCount();

for (int i = 0; i < numColumns-1; i++) {
TableColumn col=colModel.getColumn(i);

col.sizeWidthToFit();
col.setPreferredWidth(col.getWidth()+5);
col.setMaxWidth(col.getWidth()+5);


}

JScrollPane sp=new JScrollPane(table);
setLayout(new BorderLayout());

add(sp,BorderLayout.CENTER);

}

public void dispose(){
tableModel.stopRequest();
}

protected void finalize() throws Throwable{
dispose();
}

public static JFrame createFramedInstance(){
final ThreadViewer viewer=new ThreadViewer();

final JFrame f=new JFrame("ThreadViewer");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
f.setVisible(false);
f.disable();
viewer.dispose();
}
});

f.setContentPane(viewer);
f.setSize(500,300);
f.setVisible(true);

return f;
}

public static void main(String args[]){
JFrame f=ThreadViewer.createFramedInstance();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});


Object lock=new Object();
synchronized(lock){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}

}

...全文
176 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fxbird 2006-02-08
  • 打赏
  • 举报
回复
ding
fxbird 2006-02-06
  • 打赏
  • 举报
回复
up
fxbird 2006-02-04
  • 打赏
  • 举报
回复
谢了,过年好
执假以为真 2006-02-04
  • 打赏
  • 举报
回复
关注!
祝楼主新年好运道!吉祥如意!
我帮你顶!
fxbird 2006-02-03
  • 打赏
  • 举报
回复
up

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧