多线程为什么不对??

tondayong1981 2005-04-26 01:05:18
下面是一个求素数的多线程事例,我写在一起为什么不对,我分开写就对了。为什么///

//fault,no output
public class TwoThread extends Thread
{
public TwoThread(String str)
{
super(str);
}


public static void prime(int a,int b)
{
int i,j,half,n;
for(i=a;i<b;i++)
{
n=i;
half=n/2;
for(j=2;j<=half;j++)
if(n%j==0)break;
if(j>half)System.out.print(i+" ");
}

}


public void run()
{
while(true)
{
try
{
if(Thread.currentThread().getName()=="thread1")
{
sleep(1000);
System.out.println(getName());
prime(1,1000);
}
else if(Thread.currentThread().getName()=="thread2")
{ sleep(1000);
System.out.println(getName());
prime(1000,2000);
}
System.out.println(getName());
}catch(InterruptedException e)
{}

}
}

public static void main(String[] args)
{
TwoThread thread1=new TwoThread("The First Thread");
TwoThread thread2=new TwoThread("The second Thread");
thread1.start();
thread2.start();


}
}



//分开写就对了
//FirstThread.java
class FirstThread extends Thread
{
public void run()
{
int i,j,half,n;
for(i=2;i<1000;i++)
{
n=i;
half=n/2;
for(j=2;j<=half;j++)
if(n%j==0)break;
if(j>half)System.out.print(i+" ");
}

}
}
//SecondThread .java
class SecondThread extends Thread
{
public void run()
{
int i,j,half,n;
for(i=1000;i<10000;i++)
{
n=i;
half=n/2;
for(j=2;j<=half;j++)
if(n%j==0)break;
if(j>half)System.out.print(i+" ");
}

}
}


public class TestThread
{
public static void main(String[] args)
{
FirstThread thread1=new FirstThread();
SecondThread thread2=new SecondThread();
thread1.start();
thread2.start();

}
}

还有关于输入输出的问题,输入输出流和要传输的数据到底是什么关系,把数据写入到输出流,控制台怎么显示数据的,也就是输出流和控制台的关系。
...全文
136 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zdy_8212 2005-04-28
  • 打赏
  • 举报
回复
同步问题。
因为你开启了函数,而函数的数据进入缓存区。那么它们其中就有一个数值增长比较快。从而使用得数据产生二义。。synchronized
wokaosini 2005-04-28
  • 打赏
  • 举报
回复
哇你们都好厉害呀。 学习ing.
dext 2005-04-28
  • 打赏
  • 举报
回复
Thread.currentThread().getName().equals()
CodeFans 2005-04-27
  • 打赏
  • 举报
回复
public class TwoThread
extends Thread {
public TwoThread(String str) {
super(str);
}

public synchronized void prime(int a, int b) {
int i, j, half, n;
for (i = a; i < b; i++) {
n = i;
half = n / 2;
for (j = 2; j <= half; j++) {
if (n % j == 0) {
break;
}
}
if (j > half) {
System.out.println(i + " ");
}
}

}

public void run() {
try {
if (Thread.currentThread().getName().equals("thread1")) {
sleep(100);
System.out.println(getName());
prime(1, 1000);
}
else if (Thread.currentThread().getName().equals("thread2")) {
sleep(100);
System.out.println(getName());
prime(1000, 2000);
}
}
catch (Exception e) {}
}

public static void main(String[] args) {
TwoThread thread1 = new TwoThread("thread1");
TwoThread thread2 = new TwoThread("thread2");
thread1.start();
thread2.start();
}
}
laodujx 2005-04-26
  • 打赏
  • 举报
回复
输入输出流只是一种类而已,和控制台没有关系.
输入输出流用于读写文件,上传下载,传递信息等,
没有什么大用.搞笑的东西.
laodujx 2005-04-26
  • 打赏
  • 举报
回复
prime方法需要同步一下,+synchronized 修饰符
mickey50 2005-04-26
  • 打赏
  • 举报
回复
我好像遇到过这种情况,也找不到原因,在一起的用applet好像就没有问题,但是appacation我就没弄出来

62,614

社区成员

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

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