如何正确使用java多线程?

如何正确使用java多线程?,java,multithreading,Java,Multithreading,我在互联网上看到的许多例子几乎都是一样的: 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) { Te

我在互联网上看到的许多例子几乎都是一样的:

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();
    }
}

有没有一个例子可以像上面描述的那样工作。

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

     // 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.");
        }
        }

来自的参考Herbert Schildt的完整参考

它只是一个如何创建多线程的示例-没有人说它是某个特定场景的解决方案。“有没有一个例子可以像上面描述的那样工作。“只需创建不同的对象-您不必创建
测试
并反复使用它。因此,据我所知,应该有三种不同的“公共类Test1扩展线程,公共类Test2扩展线程,公共类Test3扩展线程,…”?是的,然后你可以在其中的每一个线程中做任何你想做的事情。作为记录,这个实现是毫无意义的,因为
run()
是同步的,这意味着一次只能有一个线程执行它。我只是想有一个真实的例子来理解使用多线程是否值得。正如在我最初的示例中一样,理解多线程是很清楚的,但我认为使用它没有任何意义。那么有没有人举过一个例子,比如thread1读取一个文件,thread2进行一些计算,然后谁先进行了计算,谁就通知用户。
     // 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.");
        }
        }