java线程

qq_35015914 2016-05-16 04:38:55
class Thread1 implements Runnable{
int sum1=0;
private int a=1;
public void run(){
while(true){
try{
Thread.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
if(a==11){
break;
}
else{
sum1=sum1+a;
a++;
}
synchronized(Example4.class){
Example4.class.notifyAll();
}
}
Thread t1=Thread.currentThread();
String t1_name=t1.getName();
System.out.println(t1_name+sum1);
}
}
class Thread2 implements Runnable{
int sum2=0;
private int a=10;
public void run(){
while(true){
try{
Thread.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
if(a==21){
break;
}
else{
sum2=sum2+a;
a++;
}
synchronized(Example4.class){
Example4.class.notifyAll();
}
}
Thread t2=Thread.currentThread();
String t2_name=t2.getName();
System.out.println(t2_name+sum2);
}
}
class Thread3 implements Runnable{
int sum3=0;
private int a=20;
public void run(){
while(true){
try{
Thread.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
if(a==31){
break;
}
else{
sum3=sum3+a;
a++;
}
synchronized(Example4.class){
Example4.class.notifyAll();
}
}
Thread t3=Thread.currentThread();
String t3_name=t3.getName();
System.out.println(t3_name+sum3);
}
}
public class Example4 {
public static void main(String[] args) {
Thread1 t1=new Thread1();
Thread2 t2=new Thread2();
Thread3 t3=new Thread3()
new Thread(t1,"线程一结果:").start();
new Thread(t2,"线程二结果:").start();
new Thread(t3,"线程三结果:").start();
}
}





这段程序怎么输出SUM1+SUM2+SUM3?
...全文
120 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35015914 2016-05-17
  • 打赏
  • 举报
回复
那应该怎么改
Holy_Zz 2016-05-17
  • 打赏
  • 举报
回复
你这里只有线程启动,之后没有操作任何变量

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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