引用 60 楼 dlnu05610 的回复: 引用 56 楼 whut0802 的回复: 引用 52 楼 yukang_ky 的回复: 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC… 对这个感兴趣,等待高人 Java codepublicclass ThreadTestextends Thread {publicvoid run() {for (int i=0; i <10; i++) { System.out.print(this.getName());try { Thread.currentThread().sleep(2000); }catch (InterruptedException e) { e.printStackTrace(); } } }publicstaticvoid main(String[] args)throws InterruptedException { ThreadTest a=new ThreadTest(); a.setName("A"); ThreadTest b=new ThreadTest(); b.setName("B"); ThreadTest c=new ThreadTest(); c.setName("C"); a.start(); b.start(); c.start(); } } 你要是这么写还不如不写,因为你是对线程的极度不熟悉,要是让你打印100次,你自己看看结果吧,cpu的时间片不是你想的那么简单的,这个题目得用同步来做,否则就不会是出现以上的结果(使用一个static类型的变量来控制打印的除外,因为考官压根就不是要考的这个)------
一个都不会,我日
------
有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…
public class TestABC implements Runnable{
private int i=1;
public synchronized void run()
{
while(i<=30)
{
if(i%3==0)
{
System.out.print(Thread.currentThread().getName());
i++;
try {
this.wait(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.notify();
}
if(i%3==1)
{
System.out.print(Thread.currentThread().getName());
i++;
try {
this.wait(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.notify();
}
else
{
System.out.print(Thread.currentThread().getName());
i++;
try{this.wait(100);}
catch(InterruptedException e)
{
e.printStackTrace();
}
this.notify();
}
}
}
public static void main(String[] args) {
TestABC t=new TestABC();
Thread th=new Thread(t);
th.setName("A");
th.start();
Thread th1=new Thread(t);
th1.setName("B");
th1.start();
Thread th2=new Thread(t);
th2.setName("C");
th2.start();
}
}
public class TestABC implements Runnable{
private int i=1;
public synchronized void run()
{
while(i<=30)
{
if(i%3==0)
{
System.out.print(Thread.currentThread().getName());
i++;
try {
this.wait(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.notify();
}
if(i%3==1)
{
System.out.print(Thread.currentThread().getName());
i++;
try {
this.wait(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.notify();
}
else
{
System.out.print(Thread.currentThread().getName());
i++;
try{this.wait(100);}
catch(InterruptedException e)
{
e.printStackTrace();
}
this.notify();
}
}
}
public static void main(String[] args) {
TestABC t=new TestABC();
Thread th=new Thread(t);
th.setName("A");
th.start();
Thread th1=new Thread(t);
th1.setName("B");
th1.start();
Thread th2=new Thread(t);
th2.setName("C");
th2.start();
}
}
结果是ABCABCABCABCABCABCABCABCABCABC
------
ls这个靠谱
------
引用 63 楼 raul317 的回复: 引用 62 楼 raul317 的回复: 多线程的题,我感觉这样可以,100次没有问题 public class testThread implements Runnable { public void run() { for(int i=0; i <1000; i++ ) { System.out.print(Thread.currentThread().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws InterruptedException { testThread a = new testThread(); testThread b = new testThread(); testThread c = new testThread(); Thread ta = new Thread(a); ta.setName("A"); Thread tb = new Thread(c); tb.setName("B"); Thread tc = new Thread(b); tc.setName("C"); ta.start(); Thread.sleep(50); tb.start(); Thread.sleep(50); tc.start(); } } i改成 <10啊,后来没改
桂ICP备07017180号