62,635
社区成员




package Csdn;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
class AChar {
public enum Status{ A,B,C};
private Status status=Status.A;
public void B(){status=Status.B;};
public void C(){status=Status.C;};
public Status getStatus(){return status;}
}
class CharQueue extends LinkedBlockingQueue<AChar>{
}
class StatusA implements Runnable{
private CharQueue queueA;
private int count=10;
StatusA(CharQueue queueA){
this.queueA=queueA;
}
@Override
public void run() {
while(count>0){
try {
queueA.add(new AChar());
count--;
TimeUnit.MILLISECONDS.sleep(1000);
//此处睡眠防止 A连续生产10个
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class StatusB implements Runnable{
private CharQueue queueA;
private CharQueue queueB;
private int count=10;
StatusB(CharQueue queueA,CharQueue queueB){
this.queueA=queueA;
this.queueB=queueB;
}
@Override
public void run() {
while(count>0){
try {
AChar aChar;
aChar = queueA.take();
System.out.print(aChar.getStatus());
aChar.B();
queueB.add(aChar);
count--;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class StatusC implements Runnable{
private CharQueue queueC;
private CharQueue queueB;
private int count=10;
StatusC(CharQueue queueB,CharQueue queueC){
this.queueC=queueC;
this.queueB=queueB;
}
@Override
public void run() {
while(count>0){
try {
AChar aChar;
aChar = queueB.take();
System.out.print(aChar.getStatus());
aChar.C();
queueC.add(aChar);
count--;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Finish implements Runnable{
private CharQueue queueC;
private int count=10;
Finish(CharQueue queueC){
this.queueC=queueC;
}
@Override
public void run() {
while(count>0){
try {
AChar aChar;
aChar = queueC.take();
System.out.print(aChar.getStatus());
count--;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Csdn{
public static void main(String [] args){
CharQueue queueA=new CharQueue();
CharQueue queueB=new CharQueue();
CharQueue queueC=new CharQueue();
ExecutorService exec=Executors.newCachedThreadPool();
exec.execute(new StatusA(queueA));
exec.execute(new StatusB(queueA,queueB));
exec.execute(new StatusC(queueB,queueC));
exec.execute(new Finish(queueC));
}
}
package algorithm;
public class Abc implements Runnable{
//构造函数
public Abc() {
new Thread(new A()).start();
new Thread(new B()).start();
new Thread(new C()).start();
}
//自身线程
@Override
public void run() {
while(true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(this) {
notifyAll();
}
}
}
//sync
public synchronized void print(Object obj) {
System.out.println(obj.getClass().getName());
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//线程A
public class A implements Runnable {
@Override
public void run() {
while (true) {
print(this);
}
}
}
//线程B
public class B implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
print(this);
}
}
}
//线程C
public class C implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
print(this);
}
}
}
//main
public static void main(String[] args) {
new Thread(new Abc()).start();
}
}
package csdn;
/**
* 顺序 打印ABC
* @author Administrator
*
*/
public class ThreadPrint {
public static void main(String[] args) {
A a = new A();
for (int i = 0; i < 10; i++) {
MyThread mt1 = new MyThread(a);
mt1.start();
}
}
}
class A {
private char[] c = {'A', 'B', 'C'};
private int index;
public char[] getC() {
return c;
}
public synchronized void execute() {
if (index >= c.length) {
index = 0;
} else {
System.out.println(c[index++]);
}
}
}
class MyThread extends Thread {
private A a;
public MyThread(A a) {
this.a = a;
}
public void run() {
synchronized (a) {
int index = a.getC().length;
for (int i = 0; i <= index; i++) {
a.execute();
}
System.out.println("===============");
}
}
}