概述
通过线程可以让多核机器同时完成多条复杂任务,理论来说可以减少将近一半的程序运行时间。通过主线程触发子线程启动,之后主线程等待子线程执行结束的通知,如果所有子线程都完成之后主线程才会继续走下去。
版本1
主线程
package lzx;
public class AppStart {
protected static int rideResult = 0;
protected static int divisionResult = 0;
public static Object object = new Object();
public static void main(String[] args) {
int num = 10;
Runnable rideApp = new RideApp(num);
Thread rideThread = new Thread(rideApp);
System.out.println("RideApp启动");
rideThread.start();
Runnable divisionApp = new DivisionApp(num);
Thread divisionThread = new Thread(divisionApp);
System.out.println("DivisionApp启动");
divisionThread.start();
synchronized (object) {
try {
// 让主线程进入等待
object.wait();
// 判断两个子线程是否执行完毕
if (rideThread.isAlive() || divisionThread.isAlive()) {
object.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(rideResult + "t" + divisionResult);
System.out.println("得到的结果:" + (rideResult + divisionResult));
}
}
}
子线程1
package lzx;
public class RideApp implements Runnable {
private int num;
public RideApp(int num) {
this.num = num;
}
@Override
public void run() {
try {
System.out.println("RideApp模拟执行复杂操作中......");
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AppStart.rideResult = num * 5;
System.out.println("乘法计算结束:t" + AppStart.rideResult);
synchronized (AppStart.object) {
AppStart.object.notifyAll();
}
}
}
子线程2
package lzx;
public class DivisionApp implements Runnable {
private int num;
public DivisionApp(int num) {
this.num = num;
}
@Override
public void run() {
try {
System.out.println("DivisionApp模拟执行复杂操作中......");
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AppStart.divisionResult = num / 2;
System.out.println("除法计算结束:t" + AppStart.divisionResult);
synchronized (AppStart.object) {
AppStart.object.notifyAll();
}
}
}
版本2(推荐)
主线程
package lzx;
public class AppStart {
protected static int rideResult = 0;
protected static int divisionResult = 0;
public static Object object = new Object();
public static void main(String[] args) {
int num = 10;
Runnable rideApp = new RideApp(num);
Thread rideThread = new Thread(rideApp);
System.out.println("RideApp启动");
rideThread.start();
Runnable divisionApp = new DivisionApp(num);
Thread divisionThread = new Thread(divisionApp);
System.out.println("DivisionApp启动");
divisionThread.start();
synchronized (object) {
try {
// 让主线程进入等待(等待这俩线程死亡)
rideThread.join();
divisionThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(rideResult + "t" + divisionResult);
System.out.println("得到的结果:" + (rideResult + divisionResult));
}
}
}
子线程1
package lzx;
public class RideApp implements Runnable {
private int num;
public RideApp(int num) {
this.num = num;
}
@Override
public void run() {
try {
System.out.println("RideApp模拟执行复杂操作中......");
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AppStart.rideResult = num * 5;
System.out.println("乘法计算结束:t" + AppStart.rideResult);
}
}
子线程2
package lzx;
public class DivisionApp implements Runnable {
private int num;
public DivisionApp(int num) {
this.num = num;
}
@Override
public void run() {
try {
System.out.println("DivisionApp模拟执行复杂操作中......");
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AppStart.divisionResult = num / 2;
System.out.println("除法计算结束:t" + AppStart.divisionResult);
}
}
最后
以上就是可爱大山为你收集整理的java 多任务通过多线程处理demo的全部内容,希望文章能够帮你解决java 多任务通过多线程处理demo所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复