我又来又改了一下.
改为:
......
for(int i=0; i<portNum; i++)
{
multiThread[i] = new Thread();
multiThread[i].start();
}
......
线程是建立了,但这些线程没做什么工作.
public void run
{
//这里不知道怎么写了?
multiThread[i] = Socket sss = new Socket(localhost,80);
//这样写也有错.
}
改为:
......
for(int i=0; i<portNum; i++)
{
multiThread[i] = new Thread();
multiThread[i].start();
}
......
线程是建立了,但这些线程没做什么工作.
public void run
{
//这里不知道怎么写了?
multiThread[i] = Socket sss = new Socket(localhost,80);
//这样写也有错.
}
import java.awt .* ;public class TestRunnable extends Applet implements Runnable
{
Label prompt1=new Label ("第一个子线程"); //标签1
Label prompt2=new Label ("第二个字线程"); //标签2
TextField threadFirst=new TextField (14);; //文本框1
TextField threadSecond=new TextField (14); //文本框2
Thread thread1,thread2; //两个Thread的线程对象
int count1=0,count2; //两个计数器
public void init()
{
add(prompt1);
add(threadFirst);
add(prompt2);
add(threadSecond);
}
public void start()
{ //创建线程对象,具有当前类的run()方法,并用字符串指定线程对象的名字
thread1=new Thread(this,"FirstThread"); //这里建了两个线程
thread2=new Thread(this,"SecondThread");
thread1.start(); //启动线程对象,进入就绪状态
thread2.start();
}
public void run()
{
String curRunning;
while(true)
{
try
{ //使当前活动线程休眠0到3秒
Thread.sleep((int)(Math.random()*3000));
}
catch(InterruptedException e){}
curRunning=Thread.currentThread ().getName();
if(curRunning.equals ("FirstThread"))
{
count1++;
threadFirst.setText ("线程1第"+count1+"次被调度");
}
else if(curRunning.equals ("SecondThread"))
{
count2++;
threadSecond.setText ("线程2第"+count2+"次被调度");
}
} //while循环结束
} //run()方法结束
}
//run()中,只判断了当前是哪个线程在运行..然后+1
//如果我想Thread1进行socket()连接,连接socket(localhost,80)
//Thread2也进行socket()连接,连接socket(localhost,21)在run里该怎么改??? Thread1 = Socket sss = new Socket(localhost,80);
Thread2 = Socket sss = new Socket(localhost,21); //这样不行...
multiThread[i] = new Thread();
multiThread[i].start();
你新建立的线程名是叫Thread吗,那可是系统标准线程,你只是起了个标准线程而已!