java多线程输出abc,java多线程输出数字

http://www.itjxue.com  2023-01-21 20:51  来源:未知  点击次数: 

java多线程编程之连续打印abc的几种解法

package com.demo.test;/**

* 基于两个lock实现连续打印abcabc....

* @author lixiaoxi

* */public class TwoLockPrinter implements Runnable { // 打印次数

private static final int PRINT_COUNT = 10; // 前一个线程的打印锁

private final Object fontLock; // 本线程的打印锁

private final Object thisLock; // 打印字符

private final char printChar; public TwoLockPrinter(Object fontLock, Object thisLock, char printChar) { this.fontLock = fontLock; this.thisLock = thisLock; this.printChar = printChar;

}

@Override public void run() { // 连续打印PRINT_COUNT次

for (int i = 0; i PRINT_COUNT; i++) { // 获取前一个线程的打印锁

synchronized (fontLock) { // 获取本线程的打印锁

synchronized (thisLock) { //打印字符 System.out.print(printChar); // 通过本线程的打印锁唤醒后面的线程

// notify和notifyall均可,因为同一时刻只有一个线程在等待 thisLock.notify();

} // 不是最后一次则通过fontLock等待被唤醒 // 必须要加判断,不然虽然能够打印10次,但10次后就会直接死锁

if(i PRINT_COUNT - 1){ try { // 通过fontLock等待被唤醒 fontLock.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

} public static void main(String[] args) throws InterruptedException { // 打印A线程的锁

Object lockA = new Object(); // 打印B线程的锁

Object lockB = new Object(); // 打印C线程的锁

Object lockC = new Object();

// 打印a的线程

Thread threadA = new Thread(new TwoLockPrinter(lockC, lockA, 'A')); // 打印b的线程

Thread threadB = new Thread(new TwoLockPrinter(lockA, lockB, 'B')); // 打印c的线程

Thread threadC = new Thread(new TwoLockPrinter(lockB, lockC, 'C')); // 依次开启a b c线程 threadA.start();

Thread.sleep(100); // 确保按顺序A、B、C执行 threadB.start();

Thread.sleep(100);

threadC.start();

Thread.sleep(100);

}

}

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

不知道你是不是这个意思。

有多个线程,每一个要输出的时间不固定。但你想要输入出,是个有序的。即轮到那个线程,取值都是有序的。

如果是这样,你可以将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[index++];

}

}

Values.getInstance().getValue()

这样用单例模式,可以顺次取得。

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[p]?!=?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[p]?!=?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[p]?!=?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();

}

}

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;i10;i++){

printABC.print(0);

}

}

}

class?BThread?extends?Thread{

@Override

public?void?run()?{

for(int?i=0;i10;i++){

printABC.print(1);

}

}

}

class?CThread?extends?Thread{

@Override

public?void?run()?{

for(int?i=0;i10;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 CBA BAC

public?class?ThreadTest{

????public?static?void?main(String[]?args)?{

????????String[]?prints={"ABC","CAB","BCA"};

????????Thread?thread;

????????for?(int?i?=?0;?i??3;?i++)?{

????????????thread=new?Thread(new?MyRunable(i+1,?prints[i]));

????????????thread.start();

????????}

????

?????}

?

????static?class?MyRunable?implements?Runnable{

????????private?String?str;????

????????private?int?index;

????????public?MyRunable(Integer?index,String?str)?{

????????????//?TODO?Auto-generated?constructor?stub

????????????this.str=str;

????????????this.index=index;

????????}

????????@Override

????????public?void?run()?{

????????????//?TODO?Auto-generated?method?stub

????????????System.out.println("Thread?"+index+"?start-------------"+str);

????????}

????????

????}

}

(责任编辑:IT教学网)

更多

推荐windows vista文章