多个线程执行不同的任务 [英] multiple threads performing different tasks
本文介绍了多个线程执行不同的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我第一次编写多线程程序. 我怀疑我将创建的多个线程将指向相同的run方法并执行用run()编写的任务. 但我希望不同的线程执行不同的任务 例如1个线程将在数据库中插入其他更新等. 我的问题是如何创建执行不同任务的不同线程
This is the first time I am writing multiple threaded program. I have a doubt that multiple thread which I'l create will point to the same run method and perform the task written in run(). but I want different threads to perform different tasks e.g 1 thread will insert into database other update and etc. My question is how to create different threads that will perform different tasks
推荐答案
创建执行不同工作的线程:
Create your threads that do different jobs:
public class Job1Thread extends Thread {
@Override
public void run() {
// Do job 1 here
}
}
public class Job2Thread extends Thread {
@Override
public void run() {
// Do job 2 here
}
}
启动您的线程,它们将为您工作:
Start your threads and they will work for you:
Job1Thread job1 = new Job1Thread();
Job2Thread job2 = new Job2Thread();
job1.start();
job2.start();
这篇关于多个线程执行不同的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文