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

搬砖任 2018-11-25 02:04:15
希望大佬能帮一下忙!感谢感谢感谢
...全文
286 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
绝对在乎妮 2018-11-27
  • 打赏
  • 举报
回复

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();
}
搬砖任 2018-11-27
  • 打赏
  • 举报
回复
谢谢各位!!!我自己写好了,但是也要感谢你们的脑细胞的付出
程序员苍何 2018-11-26
  • 打赏
  • 举报
回复
引用 2 楼 maradona1984 的回复:
[quote=引用 1 楼 苍何fly 的回复:]

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


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

目测是错的,至少不符合题意[/quote]我在机器上测了,没问题啊,求指教
maradona1984 2018-11-26
  • 打赏
  • 举报
回复

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();
}
}

}
maradona1984 2018-11-26
  • 打赏
  • 举报
回复
引用 1 楼 苍何fly 的回复:

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


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

目测是错的,至少不符合题意
_jant 2018-11-26
  • 打赏
  • 举报
回复
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();
}


}


这个应该符合楼主要求,散分把 刚写的
qq_39936465 2018-11-26
  • 打赏
  • 举报
回复
引用 4 楼 苍何fly 的回复:
[quote=引用 2 楼 maradona1984 的回复:][quote=引用 1 楼 苍何fly 的回复:]

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

已亲测,希望能帮到你!^_^
目测是错的,至少不符合题意[/quote]我在机器上测了,没问题啊,求指教[/quote] 你的程序是 线程A是一次只输出A 和等待1秒,然后运行了这个线程A 20次,而非一个线程运行的,不符合题意吧。
qq_39936465 2018-11-26
  • 打赏
  • 举报
回复

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();
		}
	}
}

程序员苍何 2018-11-25
  • 打赏
  • 举报
回复

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


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

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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