冲刺9

Day 9:多线程

多线程可以提高应用程序的性能和响应速度。以下是一个简单的Java程序,可以帮助我们创建和执行多线程:

javaCopy Code
public class MultiThread {
    private static final int THREAD_COUNT = 5;

    public static void main(String[] args) {
        for (int i = 0; i < THREAD_COUNT; i++) {
            Thread thread = new Thread(new MyRunnable(i));
            thread.start();
        }
    }

    private static class MyRunnable implements Runnable {
        private int id;

        public MyRunnable(int id) {
            this.id = id;
        }

        @Override
        public void run() {
            System.out.println("线程" + id + "开始执行!");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程" + id + "执行完毕!");
        }
    }
}

 

posted on 2023-06-11 16:10  摸鱼队  阅读(10)  评论(0)    收藏  举报