【问题标题】:How to use java multi-threading properly?如何正确使用java多线程?
【发布时间】:2018-01-26 13:26:30
【问题描述】:

我在网上的很多例子几乎都是一样的:

public class Test extends Thread {
    public synchronized void run() {
        for (int i = 0; i <= 10; i++) {
            System.out.println("i::"+i);
        }
    }

    public static void main(String[] args) {
        Test obj = new Test();

        Thread t1 = new Thread(obj);
        Thread t2 = new Thread(obj);
        Thread t3 = new Thread(obj);

        t1.start();
        t2.start();
        t3.start();
    }
}

那么为什么我要用不同的线程调用相同的任务(在 run() 方法中)三次?例如。如果我想上传一个文件,那我为什么要调用它三遍呢? 我假设我是否需要多线程:

thread t1 would do task1, e.g.:
   - update database info
thread t2 would do task2, e.g.:
   - upload file to server
thread t3 would do task3, e.g.:
   - bring a message to an user

有没有像上面描述的那样工作的例子。

【问题讨论】:

  • 这只是一个如何创建多个线程的示例——没有人说它是针对某些特定场景的解决方案。 “有没有一个例子可以像上面描述的那样工作。”只需制作不同的对象 - 你不必制作Test并一遍又一遍地使用它。
  • 据我所知应该有三种不同的“公共类Test1扩展线程,公共类Test2扩展线程,公共类Test3扩展线程,......”?
  • 是的,然后你就可以在每一个中做任何你想做的事情。
  • 郑重声明,这个实现毫无意义,因为run()是同步的,这意味着一次只能有一个线程执行。
  • 我只是想有一个真实的例子来了解是否值得使用多线程。就像我原来的例子一样——理解多线程很清楚,但我认为使用它没有任何意义。那么有没有人有一个例子,例如thread1 读取一个文件,thread2 做一些计算,然后谁先做的通知用户。

标签: java multithreading


【解决方案1】:

您可以使用下面给出的代码创建多个线程,您只需启动线程并且不需要多次调用同一个方法。如您所见,一旦启动,所有三个子线程都共享 CPU。注意 main() 中对 sleep(10000) 的调用。这会导致主线程休眠十秒钟,并确保它最后完成。

     // Create multiple threads.
         class NewThread implements Runnable
            { 
                String name;
            } // name of thread Thread t;
              NewThread(String threadname) 
              { 
                  name = threadname;
              } 
              t = new Thread(this, name); 
              System.out.println("New thread: " + t); 
              t.start(); // Start the thread 
        }
    // This is the entry point for thread.
        public void run() 
        {
            try 
            { 
                for(int i = 5; i > 0; i--)
                {
                    System.out.println(name + ": " + i); 
                    Thread.sleep(1000);
                } 
            } catch (InterruptedException e) 
            {
                System.out.println(name + "Interrupted");
            } 
            System.out.println(name + " exiting."); 
        }
    }
    class MultiThreadDemo 
    { 
        public static void main(String args[]) 
        { 
            new NewThread("One"); // start threads
            new NewThread("Two"); 
            new NewThread("Three");
        try 
        { 
            // wait for other threads to end
            Thread.sleep(10000); 
        }
        catch (InterruptedException e) 
        { 
            System.out.println("Main thread Interrupted"); 
        }
        System.out.println("Main thread exiting.");
        }
        }

参考来自赫伯特·希尔特的完整参考

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2020-01-01
    • 2015-11-03
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多