多线程!!!

daijope 2011-08-07 03:07:14
有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…

个人觉得:用到了sleep()不是好方法,打印出来了不一定就是正确的因为多线程的调度我们不可精细控制。ABC是线程的name,打印的时候用到Thread.getName();

要怎么写呢??
...全文
248 点赞 收藏 14
写回复
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
afunx 2011-08-08
顶11楼!
回复
回炉 2011-08-07
这个貌似会a! 将三个线程都加上同步锁就OK 了!
回复
meran 2011-08-07
哈 差不多 ~貌似 我用BlockingQueue写的 它那没有哦
回复
zqfddqr 2011-08-07

这么多回的我就不写了 哈哈哈哈哈
推荐一个强的 http://hi.baidu.com/dapplehou/blog/item/14e62834ebe1bc235ab5f504.html
回复
meran 2011-08-07







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


}
}


回复
meran 2011-08-07

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


}
}





用生产者消费者队列做的。。哈哈还算优雅把

回复
meran 2011-08-07
[Quote=引用 4 楼 daijope 的回复:]
引用 3 楼 zqfddqr 的回复:

晚上要还没人回就写个正确的~~~~~~~~~~~~~。。。。。。。。。。。。


请问锁住notify(),与wait()方法的对象必须是同一个对象吗?
[/Quote]

做同步当然要在同一个对象上
回复
杰克和仙豆 2011-08-07
有要求一定得用线程的ID么
回复
singlark 2011-08-07

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

}


随便写的,解决的可能不是很优雅。这是无数次的,至于10次的自己改吧...
回复
daijope 2011-08-07
[Quote=引用 1 楼 zqfddqr 的回复:]

Java code


package csdn;

/**
* 顺序 打印ABC
* @author Administrator
*
*/
public class ThreadPrint {

public static void main(String[] args) {
A a = new A();

for (int i = 0……
[/Quote]

希望你写一下。。。。
回复
daijope 2011-08-07
[Quote=引用 3 楼 zqfddqr 的回复:]

晚上要还没人回就写个正确的~~~~~~~~~~~~~。。。。。。。。。。。。
[/Quote]

请问锁住notify(),与wait()方法的对象必须是同一个对象吗?
回复
zqfddqr 2011-08-07
晚上要还没人回就写个正确的~~~~~~~~~~~~~。。。。。。。。。。。。
回复
zqfddqr 2011-08-07
呃!
发错了、、、
回复
zqfddqr 2011-08-07


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


回复
发动态
发帖子
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
社区公告
暂无公告