62,620
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
}
}
- -!!!
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");
}
}
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();
}
}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");
}
}