用java多线程编程,输出100个"hello world"

dennis0705 2009-11-11 09:35:54
一个java面试题
...全文
784 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlxyeyu 2009-11-11
  • 打赏
  • 举报
回复
面试题是垃圾,或者说楼主没说清楚~~
tanwan 2009-11-11
  • 打赏
  • 举报
回复


public class ThreadTest
{
public static void main(String[] args)
{
Threads ts = new Threads();
ts.start();
}
}
class Threads extends Thread
{
public void run()
{
for(int i=0;i<100;i++)
{
System.out.println("hello world");
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

- -!!!
彤袅 2009-11-11
  • 打赏
  • 举报
回复
观望高手中……
  • 打赏
  • 举报
回复
public class MultiThreadTest {

public static void main(String[] args) {
for(int i = 0; i < 100; i++) {
new PrintHelloWorld().start();
}
}

private static class PrintHelloWorld extends Thread {
public void run() {
System.out.println("hello world");
}
}
}

不知道这种面试题有什么意义所在?可以称之为极其垃圾的面试题!
Java_Op 2009-11-11
  • 打赏
  • 举报
回复
1L你那是循环.
LZ是要多线程.
class Test implements Runnable {
private static int count;
//.....................................
public Test() { count = 100; }
public Test(int count) {this.count = count; }
//...........................................
public void run()
{
while(count-- > 0)
{
System.out.print("Hello,World!" + " ");
Thread.yield();
}
}
}

public class Ex1 {
public static void main(String[] args)
{
Test t = new Test();
t.run();
}
}
zhuzeitou 2009-11-11
  • 打赏
  • 举报
回复
输出100个……怎么输出?同时还是间隔?什么都没讲清楚啊
closewbq 2009-11-11
  • 打赏
  • 举报
回复
这。。。。。。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) throws InterruptedException {
System.out.println("Hello-world begin");
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new Runnable() {
public void run() {
for (int i = 0; i < 100; i++)
System.out.println("hello world");

}
});
TimeUnit.SECONDS.sleep(5);
exec.shutdown();
System.out.println("Hello-world end");
}
}

哈哈
苍蝇①号 2009-11-11
  • 打赏
  • 举报
回复
for(int i=0;i<100;i++){
System.out.println("hello world");
}
SuperBoo 2009-11-11
  • 打赏
  • 举报
回复
5楼 正解
4楼 不对

我感觉面试题的意思是 让你启动100个线程
hui94781674 2009-11-11
  • 打赏
  • 举报
回复
public class HelloWorld implements Runnable{

@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Hello World!");
}
}

public static void main(String[] args) {
Thread thread = new Thread(new HelloWorld());
thread.start();
}

}
caolei_kunming 2009-11-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 bao110908 的回复:]
public class MultiThreadTest {

    public static void main(String[] args) {       
        for(int i = 0; i < 100; i++) {
            new PrintHelloWorld().start();
        }
    }
   
    private static class PrintHelloWorld extends Thread {
        public void run() {
            System.out.println("hello world");
        }       
    }
}

不知道这种面试题有什么意义所在?可以称之为极其垃圾的面试题!
[/Quote]就是这个了,不过PrintHelloWorld声明时候可以不写"static".
紫炎圣骑 2009-11-11
  • 打赏
  • 举报
回复
public class ThreadTest
{
public static void main(String[] args)
{
System.out.println("Hello-world begin");
ExecutorService exec = Executors.newScheduledThreadPool(5);
for (int i = 0; i < 100; i++)
{
exec.execute(new TTask("" + i));
try
{
TimeUnit.MILLISECONDS.sleep(30);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
exec.shutdown();

System.out.println("Hello-world end");

}
}

class TTask implements Runnable
{
private String tName = "";

public TTask(String name)
{
tName = name;
}

public void run()
{
System.out.println(Thread.currentThread().getId() + ":" + tName + ":hello world");
}
}

62,621

社区成员

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

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