用 Java这个多线程该怎么写啊 希望大佬能帮一下忙!感谢感谢感谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //先创建线程类public class MyThread implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName()); }}//测试类public class Test { public static void main(String[] args) { MyThread myThread = new MyThread(); try { for (int i = 0; i < 33; i++) { if (i < 20) { if (i >= 10 && i <= 12) {//当i>=10时执行线程C,并让其执行三遍,先执行完 Thread thread3 = new Thread(myThread); thread3.setName("C"); thread3.join(); thread3.start(); Thread.sleep(3000); } else { // //先执行线程A Thread thread1 = new Thread(myThread); thread1.setName("A"); thread1.start(); Thread.sleep(1000); } } else { //最后执行10遍线程B Thread thread2 = new Thread(myThread); thread2.setName("B"); thread2.start(); Thread.sleep(2000); } } } catch (InterruptedException e) { e.printStackTrace(); } }}已亲测,希望能帮到你!^_^ public class ThreadTest { public static void main() { new Thread(() -> { for (int i = 0; i < 20; i++) { if (i == 9) { new Thread(() -> { for (int j = 0; j < 3; j++) { System.out.println(String.format("当前线程:[%s],次数:[%s]", "C", j + 1)); sleep(3000L); } }).start(); } System.out.println(String.format("当前线程:[%s],次数:[%s]", "A", i + 1)); sleep(1000L); } }).start(); new Thread(() -> { for (int i = 0; i < 10; i++) { System.out.println(String.format("当前线程:[%s],次数:[%s]", "B", i + 1)); sleep(2000L); } }).start(); sleep(21000L); } private static void sleep(long time) { try { Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } }} public class test10 { public static void main(String args[]) { A a=new A(); MythreadA mt1 = new MythreadA(a,"A",1000,20); MythreadB mt2 = new MythreadB("B", 2000, 10); MythreadC mt3 = new MythreadC(a,"C", 3000, 3); new Thread(mt1).start(); new Thread(mt2).start(); new Thread(mt3).start(); }}class A { private int times; public int getTimes() { return times; } public void setTimes(int times) { this.times = times; }}class MythreadA implements Runnable { private A a=null; private String name; private int time, times; public MythreadA(A a, String name, int time, int times) { super(); this.a = a; this.name = name; this.time = time; this.times = times; } public void run() { for (int i = 0; i < times; i++) { try { a.setTimes(i); Thread.sleep(time); System.out.println(name); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}class MythreadB implements Runnable { private String name; private int time, times; public MythreadB(String name, int time, int times) { super(); this.name = name; this.time = time; this.times = times; } public void run() { for (int i = 0; i < times; i++) { try { Thread.sleep(time); System.out.println(name); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}class MythreadC implements Runnable { private A a=null; private String name; private int time, times; public MythreadC(A a, String name, int time, int times) { super(); this.a = a; this.name = name; this.time = time; this.times = times; } public void run() { try { while (a.getTimes() < 10) { Thread.sleep(100); } for (int i = 0; i < times; i++) { Thread.sleep(time); System.out.println(name); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} 目测是错的,至少不符合题意我在机器上测了,没问题啊,求指教你的程序是 线程A是一次只输出A 和等待1秒,然后运行了这个线程A 20次,而非一个线程运行的,不符合题意吧。 import java.io.IOException;public class Main { private static volatile int count_A = 0; private static volatile int count_C = 0; public static class Thread_A extends Thread { @Override public void run() { while (count_A < 20) { try { System.out.println("A"); count_A++; if (count_A == 10) { new Thread_C().start(); } sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static class Thread_B extends Thread { @Override public void run() { int i = 0; while (i < 10) { try { System.out.println("B"); i++; sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static class Thread_C extends Thread { @Override public void run() { while (count_C < 3) { try { System.out.println("C"); count_C++; sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) throws IOException { new Thread_A().start(); new Thread_B().start(); }}这个应该符合楼主要求,散分把 刚写的 public static void main(String[] args){ final int[] countA = {0}; final int[] countB = {0}; final int[] countC = {0}; Thread c = new Thread(new Runnable() { @Override public void run() { while (countC[0] < 3){ System.out.println("C"); countC[0] = countC[0] +1; try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); Thread a = new Thread(new Runnable() { @Override public void run() { while (countA[0] < 20){ if (countA[0] == 9) c.start(); System.out.println("A"); countA[0] = countA[0]+1; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); Thread b = new Thread(new Runnable() { @Override public void run() { while (countB[0] < 10){ System.out.println("B"); countB[0] = countB[0] +1; try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); a.start(); b.start(); } 请问:“哈尔滨java培训讲师的课时费是多少?” 一段简单代码运行的古怪问题,大家帮帮忙啊~~ 新手问题 关于JVM和JRE Graphics与JPanel是什么关系? 关于JDK配置正常,程序仍不能运行? 我们学校的教材,大家看看这本书怎么样? 多维数组 JAVA 中的向量主要用途在哪里 那里有Jbuilder8的中文使用说明或者书 谁知道那里有jbuild5pdf下载 送分!! JAVA读取一个正在不断写入的log文件 thinking in java中关于异常的一点疑问
//先创建线程类
public class MyThread implements Runnable { @Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}//测试类
public class Test {
public static void main(String[] args) {
MyThread myThread = new MyThread(); try {
for (int i = 0; i < 33; i++) {
if (i < 20) {
if (i >= 10 && i <= 12) {//当i>=10时执行线程C,并让其执行三遍,先执行完
Thread thread3 = new Thread(myThread);
thread3.setName("C");
thread3.join();
thread3.start();
Thread.sleep(3000);
} else {
// //先执行线程A
Thread thread1 = new Thread(myThread);
thread1.setName("A");
thread1.start();
Thread.sleep(1000);
} } else {
//最后执行10遍线程B
Thread thread2 = new Thread(myThread);
thread2.setName("B");
thread2.start();
Thread.sleep(2000);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
已亲测,希望能帮到你!^_^
public class ThreadTest { public static void main() {
new Thread(() -> {
for (int i = 0; i < 20; i++) {
if (i == 9) {
new Thread(() -> {
for (int j = 0; j < 3; j++) {
System.out.println(String.format("当前线程:[%s],次数:[%s]", "C", j + 1));
sleep(3000L);
}
}).start();
}
System.out.println(String.format("当前线程:[%s],次数:[%s]", "A", i + 1));
sleep(1000L);
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
System.out.println(String.format("当前线程:[%s],次数:[%s]", "B", i + 1));
sleep(2000L);
}
}).start();
sleep(21000L);
} private static void sleep(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}}
public class test10 { public static void main(String args[]) {
A a=new A();
MythreadA mt1 = new MythreadA(a,"A",1000,20);
MythreadB mt2 = new MythreadB("B", 2000, 10);
MythreadC mt3 = new MythreadC(a,"C", 3000, 3);
new Thread(mt1).start();
new Thread(mt2).start();
new Thread(mt3).start();
}
}class A {
private int times; public int getTimes() {
return times;
} public void setTimes(int times) {
this.times = times;
}}class MythreadA implements Runnable {
private A a=null;
private String name;
private int time, times; public MythreadA(A a, String name, int time, int times) {
super();
this.a = a;
this.name = name;
this.time = time;
this.times = times;
} public void run() {
for (int i = 0; i < times; i++) {
try {
a.setTimes(i);
Thread.sleep(time);
System.out.println(name);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
}class MythreadB implements Runnable {
private String name;
private int time, times; public MythreadB(String name, int time, int times) {
super();
this.name = name;
this.time = time;
this.times = times;
} public void run() {
for (int i = 0; i < times; i++) {
try { Thread.sleep(time);
System.out.println(name);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
}class MythreadC implements Runnable {
private A a=null;
private String name;
private int time, times; public MythreadC(A a, String name, int time, int times) {
super();
this.a = a;
this.name = name;
this.time = time;
this.times = times;
} public void run() {
try {
while (a.getTimes() < 10) {
Thread.sleep(100);
}
for (int i = 0; i < times; i++) { Thread.sleep(time);
System.out.println(name); }
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static volatile int count_A = 0;
private static volatile int count_C = 0; public static class Thread_A extends Thread {
@Override
public void run() {
while (count_A < 20) {
try {
System.out.println("A");
count_A++;
if (count_A == 10) {
new Thread_C().start();
}
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
} public static class Thread_B extends Thread {
@Override
public void run() {
int i = 0;
while (i < 10) {
try {
System.out.println("B");
i++;
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} public static class Thread_C extends Thread {
@Override
public void run() {
while (count_C < 3) {
try {
System.out.println("C");
count_C++;
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) throws IOException {
new Thread_A().start();
new Thread_B().start();
}
}
这个应该符合楼主要求,散分把 刚写的
public static void main(String[] args){ final int[] countA = {0};
final int[] countB = {0};
final int[] countC = {0};
Thread c = new Thread(new Runnable() {
@Override
public void run() {
while (countC[0] < 3){
System.out.println("C");
countC[0] = countC[0] +1;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}); Thread a = new Thread(new Runnable() {
@Override
public void run() {
while (countA[0] < 20){
if (countA[0] == 9)
c.start();
System.out.println("A");
countA[0] = countA[0]+1;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
}); Thread b = new Thread(new Runnable() {
@Override
public void run() {
while (countB[0] < 10){
System.out.println("B");
countB[0] = countB[0] +1;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
a.start();
b.start();
}