java多线程顺序打印abc(在主线程中开启3个子线程,分别实现打印字符串)

本文目录
- 在主线程中开启3个子线程,分别实现打印字符串
- java写三个线程分别输出a,b,c10次要求用wait和notify方法实现依次输出..
- java三个线程ABC输出ABC CBA BAC
- 写一个程序三个线程分别输出A,B,C, 顺序输出ABC十次,用java实现
- Java 多线程 lock condition问题,3个线程循环打印ABCABCABC.
- JAVA程序编写:输入三个数a,b,c,要求按由小到大的顺序输出
- 有1-26个数字和a-z字母,用Java多线程实现先输出2和数字再输出2个字母 ,用wait()和notify()
- java编写两个线程,要求一个输出打印1-52,一个输出打印a-z打印顺序要12a34b56c51 52 z
- java多线程中怎么依次循环输出字母A,B,C
在主线程中开启3个子线程,分别实现打印字符串
public class Demo {
//(1) "A","B", "C" (2) "AA","BB","CC" (3) "ABC"--》"CBA"--》"BAC"
public static void main(String args)throws Exception{
String str = "ABC";
getThread(str);
String arr1 = {"AA","BB","CC"};
getThread(arr1);
String arr2 = {"ABC","CBA","BAC"};
getThread(arr2);
}
private static《T》 void getThread(T t) throws InterruptedException{
Thread thread = new Thread(){
public synchronized void run(){
if(t.getClass() == String.class){
for(int i = 0; i 《 t.toString().length(); i++){
System.out.print(t.toString().charAt(i)+" ");
}
}else if(t.getClass() == String.class){
String) t;
for(int i = 0; i 《 arr.length; i++){
System.out.print(arr+" ");
}
}
}
};
thread.start();
thread.join();
System.out.println();
}
}
java写三个线程分别输出a,b,c10次要求用wait和notify方法实现依次输出..
package file.system.demo.thread;
public class PrintABC {
private int index=0;
public synchronized void print(int n){
try {
while(index!=n){
wait();
}
//index=0打印a
if(index==0)
System.out.print("a");
//index=1打印b
if(index==1)
System.out.print("b");
//index=2打印c
if(index==1)
System.out.print("c");
index = (n+1)%3;
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package file.system.demo.thread;
public class AllThread {
private PrintABC printABC;
public AllThread (PrintABC printABC) {
this.printABC=printABC;
}
class AThread extends Thread{
@Override
public void run() {
for(int i=0;i《10;i++){
printABC.print(0);
}
}
}
class BThread extends Thread{
@Override
public void run() {
for(int i=0;i《10;i++){
printABC.print(1);
}
}
}
class CThread extends Thread{
@Override
public void run() {
for(int i=0;i《10;i++){
printABC.print(2);
}
}
}
static class Test{
public static void main(String args) {
AllThread allThread = new AllThread(new PrintABC());
allThread.new AThread().start();
allThread.new BThread().start();
allThread.new CThread().start();
}
}
}
java三个线程ABC输出ABC CBA BAC
package p1;
/**
* 在主线程中开启3个子线程,分别实现打印字符串 012 345 678 (3) "ABC"--》"CBA"--》"BAC"
*
* @author Administrator
*
*/
public class OpenThreeThread_three extends Thread {
private static Object LOCK = new Object();
private static int p = 0;
private static int notifyarr = { 1, 2, 3, 3, 2, 1, 2, 1, 3 };
public static void main(String args) {
OpenThreeThread_three A = new OpenThreeThread_three() {
public void run() {
for (int i = 0; i 《 3; i++) {
synchronized (LOCK) {
while (notifyarr != 1) {
LOCK.notifyAll();
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("A" + i);
p++;
LOCK.notifyAll();
}
}
}
};
OpenThreeThread_three B = new OpenThreeThread_three() {
public void run() {
for (int i = 0; i 《 3; i++) {
synchronized (LOCK) {
while (notifyarr != 2) {
LOCK.notifyAll();
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("B" + i);
p++;
LOCK.notifyAll();
}
}
}
};
OpenThreeThread_three C = new OpenThreeThread_three() {
public void run() {
for (int i = 0; i 《 3; i++) {
synchronized (LOCK) {
while (notifyarr != 3) {
LOCK.notifyAll();
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("C" + i);
p++;
LOCK.notifyAll();
}
}
}
};
A.setName("A");
B.setName("B");
C.setName("C");
/*
* A.setPriority(4); B.setPriority(5); C.setPriority(3);
*/
A.start();
B.start();
C.start();
}
}
写一个程序三个线程分别输出A,B,C, 顺序输出ABC十次,用java实现
package T;
public class Test {
public static void main(String args) {
Thread t1 = new test1();
Thread t2 = new test2();
Thread t3 = new test3();
t1.start();
t2.start();
t3.start();
}
}
class test1 extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i 《= 9; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("A");
}
}
}
class test2 extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i 《= 9; i++) {
try {
Thread.sleep(105);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("B");
}
}
}
class test3 extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i 《= 9; i++) {
try {
Thread.sleep(110);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("C");
}
}
}
Java 多线程 lock condition问题,3个线程循环打印ABCABCABC.
你这程序运算出来只有三种答案:A,AB,ABC。
说下输出A的例子:
这种情况是Thread(b)和Thread(c)先执行后,都会wait等待,而Thread(a)线程后执行时,会唤醒new Thread(a).condition_B.signal();但是与Thread(b).condition_B无关。所有此时三组线程全部等待。
解决办法就是static condition_B,共享数据condition_B。
JAVA程序编写:输入三个数a,b,c,要求按由小到大的顺序输出
实现思路:实际上就是先输入三个数,之后分别和另外两个数比较,之后从小到大进行数值替换,之后分别输出即可。
import javax.swing.JOptionPane;
public class Arrange{
public static void main (String args){
String str;
int x,y,z;
int temp;
str=JOptionPane.showInputDialog("请输入第一个数");
x=Integer.parseInt (str);
str=JOptionPane.showInputDialog("请输入第二个数");
y=Integer.parseInt (str);
str=JOptionPane.showInputDialog("请输入第三个数");
z=Integer.parseInt (str);
if(x》y) {
temp = y;
y = x;
x = temp;
}
if(y》z){
temp = y;
y = z;
z = temp;
}
if(x》y){
temp = y;
y = x;
x = temp;
}
System.out.println("从小到大排列="+x+" "+y+" "+z);
}
}
有1-26个数字和a-z字母,用Java多线程实现先输出2和数字再输出2个字母 ,用wait()和notify()
package test;
import java.util.Date;
public class Test {
public static void main(String args) {
Print p = new Print();
new numThread(p).start();
new charThread(p).start();
}
}
class Print {
boolean boo = true;
char ch = ’A’;
int num = 1;
public synchronized void printNum() {
if (boo) {
try {
wait();
} catch (Exception e) {
}
System.out.print(num++);
System.out.print(num++);
}
boo = false;
notify();
if (num == 52)
num++;
}
public synchronized void printChar() {
if (!boo) {
try {
wait();
} catch (Exception e) {
}
System.out.print(ch++);
System.out.print(ch++);
}
boo = true;
notify();
}
}
class numThread extends Thread {
Print p = null;
public numThread(Print p) {
this.p = p;
}
public void run() {
while (p.num 《= 53)
p.printNum();
}
}
class charThread extends Thread {
Print p = null;
public charThread(Print p) {
this.p = p;
}
public void run() {
while (p.ch 《= ’Z’)
p.printChar();
}
}
java编写两个线程,要求一个输出打印1-52,一个输出打印a-z打印顺序要12a34b56c51 52 z
两个线程一个线程打印数字,一个线程用来打印字母,这样的话打印出来的顺序是数字和字母夹杂在一起的,而不是楼主所指的12a34b56c......51 52 z
java多线程中怎么依次循环输出字母A,B,C
不知道你是不是这个意思。
有多个线程,每一个要输出的时间不固定。但你想要输入出,是个有序的。即轮到那个线程,取值都是有序的。
如果是这样,你可以将A,B,C这样的值放到一个类中。
public class Values {
private char chars = { ’A’, ’B’, ’C’ };
private int index = 0;
public static Values _instance = null;
private Values() {
}
public static Values getInstance() {
if (_instance == null) {
_instance = new Values();
}
return _instance;
}
public char getValue() {
if (index 》= chars.length) {
return ’ ’;
}
return chars;
}
}
Values.getInstance().getValue()
这样用单例模式,可以顺次取得。

更多文章:
二叉树中序遍历递归算法(建立二叉树,并实现先序中序后序,用递归算法)
2026年10月15日 09:00
java多线程顺序打印abc(在主线程中开启3个子线程,分别实现打印字符串)
2026年10月15日 08:50
theadj表示一类人(the + adj 表示名词短语的形态中,其相当于单数还是复数呢如the old)
2026年10月15日 07:00
mysql多人编辑(mysql如何创建多个实例,求步骤,谢谢,急求)
2026年10月15日 06:00
webservice有几种实现方式(调用ws(webservice)接口时可以不用gsoap么还有什么实现方式)
2026年10月15日 03:50
java中如何使用正则表达式(java中如何用正则表达式判断这样的东西)
2026年10月15日 03:10






