当前位置: 代码迷 >> Java相关 >> 请教一个线程的问题
  详细解决方案

请教一个线程的问题

热度:87   发布时间:2006-08-28 20:42:41.0
请教一个线程的问题

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class Scaner extends JFrame
{
private JLabel jl1,jl2,jl3,jl4,jl5,jl6;
private JButton jb1,jb2;
private JTextArea jta;
private JTextField jtf1,jtf2,jtf3,jtf4;
private JPanel jp1,jp2,jp3;
private JScrollPane jsp;
private InetAddress ina;
private int ijtf1,ijtf2,ijtf3,ijtf4;
private ThreadScan ts;
public Scaner()
{
Container c=getContentPane();

try
{
ina=InetAddress.getLocalHost();
}
catch (Exception e)
{
return;
}

jl1=new JLabel("IP或者域名:");
jl2=new JLabel("端口范围:");
jl3=new JLabel("~");
jl4=new JLabel("扫描结果:");
jl5=new JLabel("线程数(1~10):");
jl6=new JLabel();
jl6.setLayout(new FlowLayout(FlowLayout.LEFT));

jp1=new JPanel();
jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
jp2=new JPanel();
jp2.setLayout(new FlowLayout(FlowLayout.LEFT));
jp3=new JPanel();
jp3.setLayout(new FlowLayout(FlowLayout.RIGHT));

jtf1=new JTextField(10);
jtf2=new JTextField(4);
jtf3=new JTextField(4);
jtf4=new JTextField(7);

jta=new JTextArea(30,48);
jsp=new JScrollPane(jta);
jb1=new JButton("确定");
jb2=new JButton("取消");

jp1.add(jl1);jp1.add(jtf1);jp1.add(jl2);jp1.add(jtf2);jp1.add(jl3);jp1.add(jtf3);jp1.add(jl5);jp1.add(jtf4);
jp2.add(jl4);jp2.add(jsp);
jp3.add(jl6);jp3.add(jb1);jp3.add(jb2);
c.add(jp1,BorderLayout.NORTH);
c.add(jp2,BorderLayout.CENTER);
c.add(jp3,BorderLayout.SOUTH);
setSize(550,300);
setResizable(false);
setLocation(400,300);
show();

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jtf1.getText().length()!=0||(jtf2.getText().length()!=0&&jtf3.getText().length()!=0))
caclu();
else
JOptionPane.showMessageDialog(null,"没有输入数据","错误提示",JOptionPane.ERROR_MESSAGE);
}
});
}
public static void main(String args[])
{
new Scaner();
}
public void caclu()
{
int count=0;
ijtf2=Integer.parseInt(jtf2.getText());
ijtf3=Integer.parseInt(jtf3.getText());
ijtf4=Integer.parseInt(jtf4.getText());
ts=new ThreadScan(jta,ijtf2,ijtf3,ijtf4,ina,jl6);
while(count<ijtf4){new Thread(ts).start();count++;}
//if((ijtf4<10)||(ijtf4<1))JOptionPane.showMessageDialog(null,"数据超出范围","错误提示",JOptionPane.ERROR_MESSAGE);
}

};
class ThreadScan implements Runnable
{
private int ijtf2,ijtf3,ijtf4;
private JLabel jl6;
private JTextArea jta;
private InetAddress ina;
int i;
Socket theTCPsocket;
public ThreadScan(JTextArea jta,int ijtf2,int ijtf3,int ijtf4,InetAddress ina,JLabel jl6)
{
this.jta=jta;this.ijtf2=ijtf2;this.ijtf3=ijtf3;this.ijtf4=ijtf4;this.ina=ina;this.jl6=jl6;
}
public void run()
{
i=ijtf2;
while(i<ijtf3)
{System.out.println(Thread.currentThread().getName()+" "+ (i++));
try
{
theTCPsocket=new Socket(ina,i);
//theTCPsocket.close();
jta.append(Thread.currentThread().getName()+"扫描状态:正在扫描 "+i+" 端口");
jta.append(" "+i);
switch(i)
{
case 21:
jta.append("(FTP)");
break;
case 23:
jta.append("(TELNET)");
break;
case 25:
jta.append("(SMTP)");
break;
case 80:
jta.append("(HTTP)");
break;
case 110:
jta.append("(POP)");
break;
case 139:
jta.append("(netBIOS)");
break;
default:i++;
}
}
catch (Exception e)
{return;}
}
}
};
这一句代码i=ijtf2;如果放在run()方法里打印的全部是1,可是如果放在别的地方,可以打印出1~10;这是为什么呢?
第二,我本来想他可以打印从ijtf2~ijtf3的全部数据,可是却只能打印出10个这个是为什么呢?
请各位指教!!!

搜索更多相关的解决方案: 线程  

----------------解决方案--------------------------------------------------------
请问是为什么呢?请各位指教下哈!
----------------解决方案--------------------------------------------------------
关注中。。。

----------------解决方案--------------------------------------------------------
好好把线程的内容看一下就可以了
----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------

我觉得好象错误似乎出现在线程的这个类里啊?是不是呢?可我看到有写例子和我写的差不多,却可以跑起来,我的却不行为什么呢?
请指教下!!!


----------------解决方案--------------------------------------------------------
老兄,我很奇怪,在这里System.out.println(Thread.currentThread().getName()+" "+ (i++));这里明明是i++;可为什么全部打印出来的是1呢?
----------------解决方案--------------------------------------------------------
每次都执行了i=ijtf2;



ijtf2是不是1啊 ?
我没找到

----------------解决方案--------------------------------------------------------
那个1是自己输入的,不好意思,我忘了说!!!

----------------解决方案--------------------------------------------------------
你的run方法每次都执行了i=ijtf2;
所以每次输出1

----------------解决方案--------------------------------------------------------
  相关解决方案