请老师检查

来源:3-6 自由编程

阿山123

2021-03-31 19:42:03

package com.imooc.runnablethree;

class MyThread implements Runnable {

@Override
public void run() {

char[] ch = new char[26];
int n = 0;
for (char i = 'a'; i <= 'z'; i++) {
ch[n] = i;
n++;
}
for (int i = 0; i < ch.length; i++) {
System.out.println(Thread.currentThread().getName() + ",我在打印第" + (i + 1) + "个字母 :"+ch[i]);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test {

public static void main(String[] args) {
MyThread mt=new MyThread();
Thread th=new Thread(mt,"我是线程one");
th.start();

}

}


写回答

2回答

好帮手慕小脸

2021-04-01

同学你好,

1、已完成练习,棒棒哒!继续加油!

2、优先级表示重要程度或者紧急程度.但是能不能抢到资源也是不一定,所以这里无法确保先执行哪一个线程

祝学习愉快!


0

阿山123

提问者

2021-03-31

package com.imooc.priority;

class MyThread implements Runnable{

@Override
public void run() {
for(int i=1;i<=10;i++) {
System.out.println("线程"+Thread.currentThread().getName()+"正在运行 "+i);
}
}
}
public class PriorityDemo {

public static void main(String[] args) {
// 获取主线程的优先级
// System.out.println("主线程的优先级:"+Thread.currentThread().getPriority());
MyThread mt=new MyThread();
Thread th1=new Thread(mt,"我是线程one");
th1.setPriority(Thread.MAX_PRIORITY);
th1.start();
Thread th2=new Thread(mt,"我是线程two");
th2.setPriority(Thread.MIN_PRIORITY);
th2.start();
/*
* System.out.println("线程one的优先级:"+th1.getPriority());
* System.out.println("线程two的优先级:"+th1.getPriority());
*/
}

}

http://img.mukewang.com/climg/606476ad09a1dfb612701265.jpg老师,设置了优先级也没啥作用,那这个优先级有啥用?

0

0 学习 · 16556 问题

查看课程