【问题标题】:multiple threads performing different tasks多个线程执行不同的任务
【发布时间】:2012-06-06 06:35:23
【问题描述】:

这是我第一次编写多线程程序。 我怀疑我将创建的多个线程将指向同一个运行方法并执行用 run() 编写的任务。 但我希望不同的线程执行不同的任务 例如 1 个线程将插入数据库其他更新等。 我的问题是如何创建将执行不同任务的不同线程

【问题讨论】:

  • 实现java.lang.Runnable

标签: java multithreading


【解决方案1】:

创建执行不同工作的线程:

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
    }

}

启动您的线程,它们将为您工作:

Job1Thread job1 = new Job1Thread();
Job2Thread job2 = new Job2Thread();

job1.start();
job2.start();

【讨论】:

    【解决方案2】:

    您可以使用不同的作业创建不同的类来实现 Runnable - 只是为了开始

    【讨论】:

      【解决方案3】:

      您可以根据您的条件(插入数据库、更新等)运行 run() 方法。在初始化线程类时,在类构造函数中传递参数,它将定义该线程将为您执行的任务。

      【讨论】:

        【解决方案4】:

        /* 这个程序创建三个不同的线程来执行三个不同的任务。线程 -1 打印 A...Z,线程 2 打印 1...100,线程 3 打印 100-200。理解多线程的非常基本的程序,无需为不同的任务创建不同的类 */

        class ThreadingClass implements Runnable {
           private Thread t;
           private String threadName;
        
           ThreadingClass( String name) {
              threadName = name;
              System.out.println("Creating " +  threadName );
           }
        
           public void run() {
              System.out.println("Running " +  threadName );
              if( threadName == "Thread-1" ){
                  this.printAlpha();
              }
              else if( threadName == "Thread-2" ){
                  this.printOneToHundred();
              }
              else{
                  this.printHundredMore();
              }
           }
           public void printAlpha(){
               for(int i=65; i<=90; i++)
               {
                   char x = (char)i;
                   System.out.println("RunnableDemo: " + threadName + ", " + x);
               }
           }
           public void printOneToHundred(){
               for(int i=1; i<=100; i++)
               {
        
                   System.out.println("RunnableDemo: " + threadName + ", " + i);
               }
           }
           public void printHundredMore(){
               for(int i=100; i<=200; i++)
               {
        
                   System.out.println("RunnableDemo: " + threadName + ", " + i);
               }
           }
        
           public void start () {
              System.out.println("Starting " +  threadName );
              if (t == null) {
                 t = new Thread (this);
                 t.start ();
                 t.setName(threadName);
              }
           }
        }
        public class MultiTasking {
        
           public static void main(String args[]) {
              ThreadingClass R1 = new ThreadingClass ( "Thread-1");
        
        
              ThreadingClass R2 = new ThreadingClass ( "Thread-2");
        
        
              ThreadingClass R3 = new ThreadingClass ( "Thread-3");
              R1.start();
              R2.start();
              R3.start();
        
           }   
        }
        

        【讨论】:

          【解决方案5】:

          您可以为此使用内部类。如下所示

                class first  implements Runnable 
          
          {
          
                public void run(){  
                      System.out.println("hello by tobj");  
                       }   
                    public static void main(String args[]){  
                       first obj=new first();  
                       Thread tobj =new Thread(obj);  
                       tobj.start();
                       Thread t2 =new Thread(obj)
                       {
                            public void run()
                            {
                              System.out.println("hello by t2");
                            }
                       };
                       Thread t = new Thread(obj)
                       {
                            public void run()
                            {
                              System.out.println("hello by t");
                            }
                       };
                       t.start();
                       t2.start();
          
                   }
          }
          

          【讨论】:

          • 内部类不需要使用类的对象。但它不会阻止你这样做。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-21
          • 2018-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多