急需四个利用java多线程概念的小程序,不要复杂的,越简单越好,请各位大虾帮帮忙!!!
1个回答
展开全部
例子1:
public class ThreadTest
{
public static void main(String[] args)
{
new Thread(new ThreadDemo(1)).start();
new Thread(new ThreadDemo(2)).start();
}
}
class ThreadDemo implements Runnable
{
int x = 0;
boolean is_run = true;
public ThreadDemo(int x)
{
this.x = x;
}
public void run()
{
while(is_run)
{
System.out.println("------------------------>" + x);
}
}
}
==========================================================
例子2:
public class ThreadTest
{
public static void main(String[] args)
{
new ThreadDemo(1).start();
new ThreadDemo(2).start();
}
}
class ThreadDemo extends Thread
{
int x = 0;
boolean is_run = true;
public ThreadDemo(int x)
{
this.x = x;
}
public void run()
{
while(is_run)
{
System.out.println("------------------------>" + x);
}
}
}
====================================================
例子3:
import java.util.Timer;
import java.util.TimerTask;
public class ThreadTest
{
public static void main(String[] args)
{
ThreadDemo td1 = new ThreadDemo(1);
ThreadDemo td2 = new ThreadDemo(2);
Timer time = new Timer();
time.schedule(td1, 0, 50);//0微秒后开始执行第一次,每50微秒执行一次run()方法
time.schedule(td2, 0, 50);
}
}
class ThreadDemo extends TimerTask
{
int x = 0;
boolean is_run = true;
public ThreadDemo(int x)
{
this.x = x;
}
public void run()
{
System.out.println("------------------------>" + x);
}
}
=======================================================
例子4:
我就知道这三种实现多线程的方式~~抱歉~
public class ThreadTest
{
public static void main(String[] args)
{
new Thread(new ThreadDemo(1)).start();
new Thread(new ThreadDemo(2)).start();
}
}
class ThreadDemo implements Runnable
{
int x = 0;
boolean is_run = true;
public ThreadDemo(int x)
{
this.x = x;
}
public void run()
{
while(is_run)
{
System.out.println("------------------------>" + x);
}
}
}
==========================================================
例子2:
public class ThreadTest
{
public static void main(String[] args)
{
new ThreadDemo(1).start();
new ThreadDemo(2).start();
}
}
class ThreadDemo extends Thread
{
int x = 0;
boolean is_run = true;
public ThreadDemo(int x)
{
this.x = x;
}
public void run()
{
while(is_run)
{
System.out.println("------------------------>" + x);
}
}
}
====================================================
例子3:
import java.util.Timer;
import java.util.TimerTask;
public class ThreadTest
{
public static void main(String[] args)
{
ThreadDemo td1 = new ThreadDemo(1);
ThreadDemo td2 = new ThreadDemo(2);
Timer time = new Timer();
time.schedule(td1, 0, 50);//0微秒后开始执行第一次,每50微秒执行一次run()方法
time.schedule(td2, 0, 50);
}
}
class ThreadDemo extends TimerTask
{
int x = 0;
boolean is_run = true;
public ThreadDemo(int x)
{
this.x = x;
}
public void run()
{
System.out.println("------------------------>" + x);
}
}
=======================================================
例子4:
我就知道这三种实现多线程的方式~~抱歉~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询