62,634
社区成员




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();
}
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();
}
}
}
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 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();
}
}
}
//先创建线程类
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();
}
}
}