千峰逆战班,Day28

在千峰“逆战班”学习的第28天
今天的学习内容是线程,包括线程的组成,2种创建方式,6种状态,常用的三个方法以及线程安全中的第一种同步方式
中国加油!武汉加油!千峰加油!我自己加油!!!

作业:
1.单核CPU在任何时间点上,只能运行一个进程,宏观并行、微观串行
2.C
3.C
4.A
5.

import java.util.Random;

public class Question{
	public static void main(String[] args){
		MyThread t1 = new MyThread();
		Thread t2 = new Thread(new MyRunnable());
		t1.start();
		t2.start();
	}
	
}
class MyThread extends Thread{
	public void run(){
		for(int i = 0; i < 100; i++){
			Random r = new Random();
			int n = r.nextInt(25);
			System.out.println("数字第" + i + "次:"+ (n + 1));
		}
	}
}
class MyRunnable implements Runnable{
	public void run(){
		for(int i = 0; i < 100; i++){
			Random r = new Random();
			int n = r.nextInt(25);
			System.out.println("字母第" + i + "次:"+ (char)(n + 65));
		}
	}
}

6.为Object类对象lock上一把互斥锁,线程t1和t2只有一个可以获得lock的互斥锁标记,使得run()方法中的内容成为一个整体,不被破坏,一个线程完成再执行另一个线程。不使用的话,t1和t2会交替执行
7.C

发布了25 篇原创文章 · 获赞 0 · 访问量 903

猜你喜欢

转载自blog.csdn.net/Hydz666_/article/details/104803641