Java多线程实验

多线程是比较抽象的一个概念,其实多线程是一种运筹,单线程就像一次只干一件事情,一件事情做完再去做接下来的另外一件事,比如先煮饭,等饭熟了再炒菜。而多线程则是饭还在煮着的同时就炒菜,然后饭菜差不多同时好了。这就是充分利用等待的空余时间去同时完成多项任务的一种运筹。


多线程中最重要的两个命令应该是start()和join()

start()就是开始一个线程,比如按下电饭煲的开关,开始做菜等。

join()就是等什么一齐好了,比如吃饭前要等饭和菜都好了才能开始吃饭。


所以大体就是这样:


Thread 做饭;

Thread 做菜;

做饭.start();

做菜.start();

做饭.join();

做菜.join();

吃饭;


package threadexperiment;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author Administrator
 */
public class ThreadExperiment extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("多线程实验");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                
                dataInput.clear();
                for(int i=0;i<100;i++){
                    dataInput.add(i);                    
                }
                
                dataOutput.clear();
                                
                //批量发动线程,百团大战
                Thread thread0=new MyThread(0,9);
                Thread thread1=new MyThread(10,19);
                Thread thread2=new MyThread(20,29);
                Thread thread3=new MyThread(30,39);
                Thread thread4=new MyThread(40,49);
                Thread thread5=new MyThread(50,59);
                Thread thread6=new MyThread(60,69);
                Thread thread7=new MyThread(70,79);
                Thread thread8=new MyThread(80,89);
                Thread thread9=new MyThread(90,99);
                
                thread0.start();
                thread1.start();
                thread2.start();
                thread3.start();
                thread4.start();
                thread5.start();
                thread6.start();
                thread7.start();
                thread8.start();
                thread9.start();
                
                try {
                    //线程批量会师
                    thread0.join();
                    thread1.join();
                    thread2.join();
                    thread3.join();
                    thread4.join();
                    thread5.join();
                    thread6.join();
                    thread7.join();
                    thread8.join();
                    thread9.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadExperiment.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                
                System.out.println("所有线程胜利会师");
                
                
                //线程批量会师后才会执行后续的程序,来个大阅兵检验            
                
                for(int i=0;i<dataOutput.size();i++){
                    System.out.println("dataOutput"+i+":"+dataOutput.get(i));
                }
                            
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
     
    public ArrayList<Integer> dataInput=new ArrayList<>();
    public ArrayList<Integer> dataOutput=new ArrayList<>();
    
    public class MyThread extends Thread{
        int startIndex;
        int endIndex;
        int currentPostion;
        public MyThread(int startIndex,int endIndex){
            this.startIndex =startIndex;
            this.endIndex =endIndex;
            this.currentPostion=startIndex;
            System.out.println("读取数组列表:第"+startIndex+"到第"+endIndex+"条记录");
        }
        @Override
        public void run(){
            while(true){
                int number=dataInput.get(currentPostion);
                dataOutput.add(number);
                System.out.println("正在读取第"+currentPostion+"条记录");
                currentPostion++;
                if(currentPostion==endIndex+1){
                    return;
                }
            }
        }
    }
    
}



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页