java线程

qq_35015914 qq_35015914 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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?
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
qq_35015914
Java 51,407 成员 86,128 内容