实现多线程有2种方法
[b]1.继承自Thread类,可以重写run()方法去覆盖去Thread中的run()方法[/b]
package thread;
import java.util.Date;
public class TestThreads extends Thread{
public int time;
public String user;
public TestThreads(int time,String user){
this.time=time;
this.user=user;
}
public void run(){
while(true){
try{
System.out.println(user+"休息"+time+"ms-"+new Date());
Thread.sleep(time);
}catch(Exception ee){
System.out.println(ee);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1=new TestThreads(10000,"jack");
Thread t2=new TestThreads(20000,"Mike");
t1.start();
t2.start();
//t1.setPriority(1);
//t2.setPriority(Thread.MAX_PRIORITY);//得到cpu执行的机会要大一点
}
}
[b]2.实现Runnable接口并实现它的run()方法[/b]
package thread;
import java.util.Date;
public class TestRunnable implements Runnable {
public int time;
public String user;
public TestRunnable(int time,String user){
this.time=time;
this.user=user;
}
public void run(){
while(true){
try{
System.out.println(user+"休息"+time+"ms-"+new Date());
Thread.sleep(time);
}catch(Exception ee){
System.out.println(ee);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1=new Thread(new TestRunnable(1000,"jack"));
Thread t2=new Thread(new TestRunnable(3000,"Mike"));
t1.start();
t2.start();
//t1.setPriority(1);
//t2.setPriority(Thread.MAX_PRIORITY);//得到cpu执行的机会要大一点
}
}
注:通过设置setPriority来设置调用线程对象的优先级
其中多线程之间是“并行交替”执行的,通过获取时间片来执行
如:
package thread;
public class TestThreadm extends Thread {
public int i;
/**
* @param args
*/
public TestThreadm(int i){
this.i=i;
}
public void run(){
for(int j=0;j<100;j++){
System.out.println("user"+i+"运行"+j);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int m=0;m<5;m++){
new TestThreadm(m).start();
}
}
}
运行截取其中一小片
user0运行65
user0运行66
user0运行67
user0运行68
user0运行69
user0运行70
user0运行71
user4运行0
user4运行1
user4运行2
user4运行3
user4运行4
user4运行5
user4运行6
user4运行7
发现是交替执行的
注:如果设置优先级(可以控制执行的次序)那么优先级高的优先执行,且可以分派的cpu执行时间就多点(先执行),相反就少点(后执行)。你可以调用 Thread 类的方法 getPriority() 和 setPriority()来存取线程的优先级,线程的优先级界于1(MIN_PRIORITY)和10(MAX_PRIORITY)之间,缺省是5(NORM_PRIORITY)。
[b]但是线程的优先级代表该线程的重要程度(不是绝对的),当有多个线程同时处于可执行状态并等待获得 CPU 时间时,线程调度系统根据各个线程的优先级来决定给谁分配 CPU 时间,优先级高的线程有更大的机会获得 CPU 时间,优先级低的线程也不是没有机会,只是机会要小一些罢了。[/b]
[b]1.继承自Thread类,可以重写run()方法去覆盖去Thread中的run()方法[/b]
package thread;
import java.util.Date;
public class TestThreads extends Thread{
public int time;
public String user;
public TestThreads(int time,String user){
this.time=time;
this.user=user;
}
public void run(){
while(true){
try{
System.out.println(user+"休息"+time+"ms-"+new Date());
Thread.sleep(time);
}catch(Exception ee){
System.out.println(ee);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1=new TestThreads(10000,"jack");
Thread t2=new TestThreads(20000,"Mike");
t1.start();
t2.start();
//t1.setPriority(1);
//t2.setPriority(Thread.MAX_PRIORITY);//得到cpu执行的机会要大一点
}
}
[b]2.实现Runnable接口并实现它的run()方法[/b]
package thread;
import java.util.Date;
public class TestRunnable implements Runnable {
public int time;
public String user;
public TestRunnable(int time,String user){
this.time=time;
this.user=user;
}
public void run(){
while(true){
try{
System.out.println(user+"休息"+time+"ms-"+new Date());
Thread.sleep(time);
}catch(Exception ee){
System.out.println(ee);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1=new Thread(new TestRunnable(1000,"jack"));
Thread t2=new Thread(new TestRunnable(3000,"Mike"));
t1.start();
t2.start();
//t1.setPriority(1);
//t2.setPriority(Thread.MAX_PRIORITY);//得到cpu执行的机会要大一点
}
}
注:通过设置setPriority来设置调用线程对象的优先级
其中多线程之间是“并行交替”执行的,通过获取时间片来执行
如:
package thread;
public class TestThreadm extends Thread {
public int i;
/**
* @param args
*/
public TestThreadm(int i){
this.i=i;
}
public void run(){
for(int j=0;j<100;j++){
System.out.println("user"+i+"运行"+j);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int m=0;m<5;m++){
new TestThreadm(m).start();
}
}
}
运行截取其中一小片
user0运行65
user0运行66
user0运行67
user0运行68
user0运行69
user0运行70
user0运行71
user4运行0
user4运行1
user4运行2
user4运行3
user4运行4
user4运行5
user4运行6
user4运行7
发现是交替执行的
注:如果设置优先级(可以控制执行的次序)那么优先级高的优先执行,且可以分派的cpu执行时间就多点(先执行),相反就少点(后执行)。你可以调用 Thread 类的方法 getPriority() 和 setPriority()来存取线程的优先级,线程的优先级界于1(MIN_PRIORITY)和10(MAX_PRIORITY)之间,缺省是5(NORM_PRIORITY)。
[b]但是线程的优先级代表该线程的重要程度(不是绝对的),当有多个线程同时处于可执行状态并等待获得 CPU 时间时,线程调度系统根据各个线程的优先级来决定给谁分配 CPU 时间,优先级高的线程有更大的机会获得 CPU 时间,优先级低的线程也不是没有机会,只是机会要小一些罢了。[/b]