weixin_47018631 2021-06-10 20:36 采纳率: 0%
浏览 61

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

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.
  • 写回答

2条回答 默认 最新

  • 关注
    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();		
    	}
    	
    }
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 易优eyoucms关于二级栏目调用的问题
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题