这个实验室的目的是创建三个线程并同时运行它们?

The purpose of this lab is to create three threads and run them at the same time. While are running they will be printing out their name in the console. By observing what is printed in the console, you can observe how the threads run and in which order.

  1. Create a class called PrintMe which implements Runnable.
    In the run method, loop 10 times: print the name of the current thread and then sleep for 2 seconds.
  2. Create a program called TestThreeThreads.
    1. In the main, create an instance of PrintMe.
    2. Create three threads using the PrintMe object.
    3. Name each thread using the setName method.
    4. Start each thread.
  3. Compile and run the TestThreeThreads program.
package java3.T13;

public class RunnableDemo {
	public static void main(String[] args) {
		
		MyThread myThread= new MyThread ();
		//创建并启动线程		
		Thread ta = new Thread(myThread);
        Thread tb = new Thread(myThread);
        Thread tc = new Thread(myThread);
		ta.start();		
		tb.start();
        tc.start();
		
	}
}

class MyThread implements Runnable{

    public void PrintMe(){
        for(int i=0;i<10;i++) {
			try {
				System.out.println(Thread.currentThread().getName()+i);
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
    }
	@Override
	public void run() {
		PrintMe();		
	}
	
}

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632