用 Java这个多线程该怎么写啊

[复制链接]
查看11 | 回复9 | 2021-1-27 06:16:19 | 显示全部楼层 |阅读模式
希望大佬能帮一下忙!感谢感谢感谢


分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:16:19 | 显示全部楼层

//先创建线程类
publicclassMyThreadimplementsRunnable{
@Override
publicvoidrun(){
System.out.println(Thread.currentThread().getName());
}
}
//测试类
publicclassTest{
publicstaticvoidmain(String[]args){
MyThreadmyThread=newMyThread();
try{
for(inti=0;i=10&&i=10时执行线程C,并让其执行三遍,先执行完
Threadthread3=newThread(myThread);
thread3.setName("C");
thread3.join();
thread3.start();
Thread.sleep(3000);
}else{
////先执行线程A
Threadthread1=newThread(myThread);
thread1.setName("A");
thread1.start();
Thread.sleep(1000);
}
}else{
//最后执行10遍线程B
Threadthread2=newThread(myThread);
thread2.setName("B");
thread2.start();
Thread.sleep(2000);
}
}
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}

已亲测,希望能帮到你!^_^
回复

使用道具 举报

千问 | 2021-1-27 06:16:19 | 显示全部楼层
引用1楼苍何fly的回复:
//先创建线程类
publicclassMyThreadimplementsRunnable{
@Override
publicvoidrun(){
System.out.println(Thread.currentThread().getName());
}
}
//测试类
publicclassTest{
publicstaticvoidmain(String[]args){
MyThreadmyThread=newMyThread();
try{
for(inti=0;i=10&&i=10时执行线程C,并让其执行三遍,先执行完
Threadthread3=newThread(myThread);
thread3.setName("C");
thread3.join();
thread3.start();
Thread.sleep(3000);
}else{
////先执行线程A
Threadthread1=newThread(myThread);
thread1.setName("A");
thread1.start();
Thread.sleep(1000);
}
}else{
//最后执行10遍线程B
Threadthread2=newThread(myThread);
thread2.setName("B");
thread2.start();
Thread.sleep(2000);
}
}
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}

已亲测,希望能帮到你!^_^

目测是错的,至少不符合题意
回复

使用道具 举报

千问 | 2021-1-27 06:16:19 | 显示全部楼层

publicclassThreadTest{
publicstaticvoidmain(){
newThread(()->{
for(inti=0;i{
for(intj=0;j{
for(inti=0;i
回复

使用道具 举报

千问 | 2021-1-27 06:16:19 | 显示全部楼层
引用2楼maradona1984的回复:Quote: 引用1楼苍何fly的回复:
//先创建线程类
publicclassMyThreadimplementsRunnable{
@Override
publicvoidrun(){
System.out.println(Thread.currentThread().getName());
}
}
//测试类
publicclassTest{
publicstaticvoidmain(String[]args){
MyThreadmyThread=newMyThread();
try{
for(inti=0;i=10&&i=10时执行线程C,并让其执行三遍,先执行完
Threadthread3=newThread(myThread);
thread3.setName("C");
thread3.join();
thread3.start();
Thread.sleep(3000);
}else{
////先执行线程A
Threadthread1=newThread(myThread);
thread1.setName("A");
thread1.start();
Thread.sleep(1000);
}
}else{
//最后执行10遍线程B
Threadthread2=newThread(myThread);
thread2.setName("B");
thread2.start();
Thread.sleep(2000);
}
}
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}

已亲测,希望能帮到你!^_^

目测是错的,至少不符合题意
我在机器上测了,没问题啊,求指教
回复

使用道具 举报

千问 | 2021-1-27 06:16:19 | 显示全部楼层

publicclasstest10{
publicstaticvoidmain(Stringargs[]){
Aa=newA();
MythreadAmt1=newMythreadA(a,"A",1000,20);
MythreadBmt2=newMythreadB("B",2000,10);
MythreadCmt3=newMythreadC(a,"C",3000,3);
newThread(mt1).start();
newThread(mt2).start();
newThread(mt3).start();
}
}
classA{
privateinttimes;
publicintgetTimes(){
returntimes;
}
publicvoidsetTimes(inttimes){
this.times=times;
}
}
classMythreadAimplementsRunnable{
privateAa=null;
privateStringname;
privateinttime,times;

publicMythreadA(Aa,Stringname,inttime,inttimes){
super();
this.a=a;
this.name=name;
this.time=time;
this.times=times;
}

publicvoidrun(){
for(inti=0;i
回复

使用道具 举报

千问 | 2021-1-27 06:16:19 | 显示全部楼层
引用4楼苍何fly的回复:Quote: 引用2楼maradona1984的回复:Quote: 引用1楼苍何fly的回复:
//先创建线程类
publicclassMyThreadimplementsRunnable{
@Override
publicvoidrun(){
System.out.println(Thread.currentThread().getName());
}
}
//测试类
publicclassTest{
publicstaticvoidmain(String[]args){
MyThreadmyThread=newMyThread();
try{
for(inti=0;i=10&&i=10时执行线程C,并让其执行三遍,先执行完
Threadthread3=newThread(myThread);
thread3.setName("C");
thread3.join();
thread3.start();
Thread.sleep(3000);
}else{
////先执行线程A
Threadthread1=newThread(myThread);
thread1.setName("A");
thread1.start();
Thread.sleep(1000);
}
}else{
//最后执行10遍线程B
Threadthread2=newThread(myThread);
thread2.setName("B");
thread2.start();
Thread.sleep(2000);
}
}
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}

已亲测,希望能帮到你!^_^

目测是错的,至少不符合题意
我在机器上测了,没问题啊,求指教

你的程序是线程A是一次只输出A和等待1秒,然后运行了这个线程A20次,而非一个线程运行的,不符合题意吧。

回复

使用道具 举报

千问 | 2021-1-27 06:16:19 | 显示全部楼层
importjava.io.IOException;
publicclassMain{
privatestaticvolatileintcount_A=0;
privatestaticvolatileintcount_C=0;
publicstaticclassThread_AextendsThread{
@Override
publicvoidrun(){
while(count_A
这个应该符合楼主要求,散分把刚写的
回复

使用道具 举报

千问 | 2021-1-27 06:16:19 | 显示全部楼层
谢谢各位!!!我自己写好了,但是也要感谢你们的脑细胞的付出
回复

使用道具 举报

千问 | 2021-1-27 06:16:19 | 显示全部楼层

publicstaticvoidmain(String[]args){
finalint[]countA={0};
finalint[]countB={0};
finalint[]countC={0};
Threadc=newThread(newRunnable(){
@Override
publicvoidrun(){
while(countC[0]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行