如何在Java中使用多个线程执行单个任务?

如果您需要让多个线程执行单个任务,只需使用一个run()方法。例如:

执行单个任务的程序,由多个线程执行

文件名:TestMultitasking1.java

class TestMultitasking1 extends Thread{  
 public void run(){  
   System.out.println("task one");  
 }  
 public static void main(String args[]){  
  TestMultitasking1 t1=new TestMultitasking1();  
  TestMultitasking1 t2=new TestMultitasking1();  
  TestMultitasking1 t3=new TestMultitasking1();  
  
  t1.start();  
  t2.start();  
  t3.start();  
 }  
}  

输出:

task one
task one
task one

执行单个任务的程序,由多个线程执行

文件名:TestMultitasking2.java

class TestMultitasking2 implements Runnable{  
public void run(){  
System.out.println("task one");  
}  
  
public static void main(String args[]){  
Thread t1 =new Thread(new TestMultitasking2());//传递 TestMultitasking2 类的匿名对象 
Thread t2 =new Thread(new TestMultitasking2());  
  
t1.start();  
t2.start();  
  
 }  
}  

输出:

task one
task one

注意:每个线程在单独的调用栈中运行。

图

如何在多线程中执行多个任务(多任务多线程)?

如果您需要让多个线程执行多个任务,可以使用多个run()方法。例如:

通过两个线程执行两个任务的程序

文件名:TestMultitasking3.java

class Simple1 extends Thread{  
 public void run(){  
   System.out.println("task one");  
 }  
}  
  
class Simple2 extends Thread{  
 public void run(){  
   System.out.println("task two");  
 }  
}  
  
 class TestMultitasking3{  
 public static void main(String args[]){  
  Simple1 t1=new Simple1();  
  Simple2 t2=new Simple2();  
  
  t1.start();  
  t2.start();  
 }  
}  

输出:

task one
task two

与上述示例相同,使用扩展Thread类的匿名类实现:

通过两个线程执行两个任务的程序

文件名:TestMultitasking4.java

class TestMultitasking4{  
 public static void main(String args[]){  
  Thread t1=new Thread(){  
    public void run(){  
      System.out.println("task one");  
    }  
  };  
  Thread t2=new Thread(){  
    public void run(){  
      System.out.println("task two");  
    }  
  };  
  
  
  t1.start();  
  t2.start();  
 }  
}  

输出:

task one
task two

与上述示例相同,使用实现Runnable接口的匿名类实现:

通过两个线程执行两个任务的程序

文件名:TestMultitasking5.java

class TestMultitasking5{  
 public static void main(String args[]){  
  Runnable r1=new Runnable(){  
    public void run(){  
      System.out.println("task one");  
    }  
  };  
  
  Runnable r2=new Runnable(){  
    public void run(){  
      System.out.println("task two");  
    }  
  };  
      
  Thread t1=new Thread(r1);  
  Thread t2=new Thread(r2);  
  
  t1.start();  
  t2.start();  
 }  
}  

输出:

task one
task two

使用两个线程打印偶数和奇数

为了使用两个线程打印偶数和奇数,我们将使用synchronized块和notify()方法。请观察以下程序。

文件名:OddEvenExample.java

// 使用两个线程打印奇数和偶数的 Java 程序。  
// 程序的时间复杂度是 O(N),其中 N 是我们要达到的数   
// 正在显示数字  
public class OddEvenExample   
{  
// 启动计数器   
int contr = 1;  
static int NUM;  
// 打印奇数的方法    
public void displayOddNumber()  
{  
// 请注意,同步块是获取所需代码所必需的   
// 输出。如果我们删除同步块,我们将得到一个异常。
synchronized (this)  
{  
// 打印数字直到 NUM   
while (contr < NUM)   
{  
// 如果 contr 是偶数则显示   
while (contr % 2 == 0)   
{  
// 处理异常句柄  
try   
{  
wait();  
}  
catch (InterruptedException ex)   
{  
ex.printStackTrace();  
}  
}  
// 打印数字   
System.out.print(contr + " ");  
//增加控制      
contr = contr + 1;  
// 通知正在等待这个锁的线程   
notify();  
}  
}  
}  
// 打印偶数的方法   
public void displayEvenNumber()  
{  
synchronized (this)  
{  
// 打印数字直到 NUM    
while (contr < NUM)   
{  
// 如果计数是奇数则显示   
while (contr % 2 == 1)   
{  
// 处理异常    
try   
{  
wait();  
}  
catch (InterruptedException ex)   
{  
ex.printStackTrace();  
}  
}  
// 打印数字    
System.out.print(contr + " ");  
//增加控制   
contr = contr +1;  
// 通知第二个线程 
notify();  
}  
}  
}  
// 主要方法   
public static void main(String[] argvs)  
{  
// 给出了 NUM 
NUM = 20;  
// 创建类 OddEvenExample 的对象    
OddEvenExample oe = new OddEvenExample();  
//创建线程th1    
Thread th1 = new Thread(new Runnable()   
{  
public void run()  
{  
// 使用线程 th1 调用方法 displayEvenNumber()  
oe.displayEvenNumber();  
}  
});  
// 创建一个线程 th2   
Thread th2 = new Thread(new Runnable()   
{  
public void run()  
{  
// 使用线程 th2 调用方法 displayOddNumber()  
oe.displayOddNumber();  
}  
});  
// 启动两个线程  
th1.start();  
th2.start();  
}  
}  

输出:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

标签: java, Java面试题, Java下载, java教程, java技术, Java学习, Java学习教程, Java语言, Java开发, Java入门教程, Java进阶教程, Java高级教程, Java笔试题, Java编程思想