一个关于线程的问题 开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推这个该如何做?请教高手 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.lang.*;public class ThreadABC{ public static void main(String[] args) { new Thread(new ABC(0)).start(); new Thread(new ABC(1)).start(); new Thread(new ABC(2)).start(); } }class ABC implements Runnable{ char a = 'A'; ABC(int i) { a = (char)(a + i); } public void run() { for(int i=0; i<3; i++) { System.out.println(a); try{ Thread.sleep(100); } catch(InterruptedException e) { } } } }这一种方法一般可以实现你要的功能,但是这个方法是危险,请楼主注意。在某些情况,这个方法打印出来的可能也不是你要的顺序,但是一般是正确的了。 1.5以后有个先进的办法创建线程.class ABC implements Runnable{ char a = 'A'; ABC(int i) { a = (char)(a + i); } public void run() { for(int i=0; i<10; i++) { System.out.println(a); try{ Thread.sleep(100); } catch(Exception e) { e.printStackTrace(); } } }public class ThreadTest{ public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(3); executorService.execute(new ABC(0)); executorService.execute(new ABC(1)); executorService.execute(new ABC(2)); //记得运行完以后把线程都删掉,要不然程序会一直运行而不会停止 executor.shutdown(); }} }记得该导入的包要导入就可以了 public class ThreadABC{ public static char last = 'C'; public static byte[] key = new byte[0]; public static void main(String[] args) { new Thread(new ABC(0,key)).start(); new Thread(new ABC(1,key)).start(); new Thread(new ABC(2,key)).start(); } }class ABC implements Runnable{ char a = 'A'; byte[] key; ABC(int i,byte[] key) { this.key = key; a = (char)(a + i); } public void run() { while(true){ synchronized(key){ while(ThreadABC.last!=(a-63)%3+65) try { key.wait(); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.println(a); ThreadABC.last = a; key.notifyAll(); } try{ Thread.sleep(100); } catch(InterruptedException e) { } } } }总算搞定了,累死我也…………………… 解释下7#的代码……用一个static函数last记录上一次打印,线程运行时判断上一个打印的是不是自己的前一个,是自己的前一个就打印,不是的话就等待,拆开来写就是下面这样synchronized(key){ if(a=='A'){ while(ThreadABC.last != 'C') try { key.wait(); } catch (InterruptedException e1) { e1.printStackTrace(); } }else while(ThreadABC.last != a-1) try { key.wait(); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.println(a); ThreadABC.last = a; key.notifyAll(); } java中写一个等式方程的困惑,请老鸟解惑! 学JSP要学哪一本书? 怎么打开一个指定的网页 有关数据库插入 【关于 SWT 的一些参数!】 好怪的一个小鸟级问题 高分!!如何改进我的软件实现中英文界面互换呀?小女先谢过啦! merge into 变量绑定问题 初学问题 一个关于SCJP1。4的问题,我吃饭回来就给分。 新人求助 思考:如果希望明确的一个线程给另外一个线程运行的机会,有哪几种方法?
{
public static void main(String[] args)
{
new Thread(new ABC(0)).start();
new Thread(new ABC(1)).start();
new Thread(new ABC(2)).start();
}
}class ABC implements Runnable
{
char a = 'A';
ABC(int i)
{
a = (char)(a + i);
}
public void run()
{
for(int i=0; i<3; i++)
{
System.out.println(a);
try{
Thread.sleep(100);
}
catch(InterruptedException e)
{
}
}
}
}
这一种方法一般可以实现你要的功能,但是这个方法是危险,请楼主注意。
在某些情况,这个方法打印出来的可能也不是你要的顺序,但是一般是正确的了。
{
char a = 'A';
ABC(int i)
{
a = (char)(a + i);
}
public void run()
{
for(int i=0; i<10; i++)
{
System.out.println(a);
try{
Thread.sleep(100);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class ThreadTest
{
public static void main(String[] args)
{
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.execute(new ABC(0));
executorService.execute(new ABC(1));
executorService.execute(new ABC(2));
//记得运行完以后把线程都删掉,要不然程序会一直运行而不会停止
executor.shutdown();
}
}
}
记得该导入的包要导入就可以了
public class ThreadABC
{
public static char last = 'C';
public static byte[] key = new byte[0];
public static void main(String[] args)
{
new Thread(new ABC(0,key)).start();
new Thread(new ABC(1,key)).start();
new Thread(new ABC(2,key)).start();
}
}class ABC implements Runnable
{
char a = 'A';
byte[] key;
ABC(int i,byte[] key)
{
this.key = key;
a = (char)(a + i);
}
public void run()
{
while(true){
synchronized(key){
while(ThreadABC.last!=(a-63)%3+65)
try {
key.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(a);
ThreadABC.last = a;
key.notifyAll();
}
try{
Thread.sleep(100);
}
catch(InterruptedException e)
{
}
}
}
}
总算搞定了,累死我也……………………
synchronized(key){
if(a=='A'){
while(ThreadABC.last != 'C')
try {
key.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}else
while(ThreadABC.last != a-1)
try {
key.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(a);
ThreadABC.last = a;
key.notifyAll();
}