Make an existing code in Java parallel/multithread

后端 未结 3 2007
孤城傲影
孤城傲影 2021-01-05 03:37

I have a very simple crawler. I want to make my current code run in a few threads. Could you provide me a little tutorial or article to help me achive this test?

I\'

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-05 03:52

    A very basic java program that will give the abstract idea of the Multi Threading..

    public class MyThread extends Thread {
    
      String word;
    
      public MyThread(String rm){
        word = rm;
      }
    
      public void run(){
    
        try {
    
          for(;;){
            System.out.println(word);
            Thread.sleep(1000);
          }
    
        } catch(InterruptedException e) {
    
          System.out.println("sleep interrupted");      
        }
      }
    
      public static void main(String[] args) {
    
        Thread t1=new MyThread("First Thread");
        Thread t2=new MyThread("Second Thread");
        t1.start();
        t2.start();
      }
    } 
    

    And the Output will be..

    First Thread
    Second Thread
    First Thread
    Second Thread
    First Thread
    

    Go with this PPT it will help you with the basics..

    Here

提交回复
热议问题