Java中使用多个线程执行单个任务

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

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

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

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

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

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 = 20; 
 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值