多线程!!!

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

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

要怎么写呢??
...全文
327 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
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]

做同步当然要在同一个对象上
阳明 to life 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("===============");
}
}
}


62,635

社区成员

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

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