千锋逆战班,Day28

在千锋逆战学习Java的第28天
加油!
今天学习了多线程的概念,创建方式,sleep()、yield()、join()方法和同步方式。

作业题:

1.一个单CPU的机器,如何同时执行多个线程?请简述其原理
宏观串行,微观并行。cpu会分配给不同的进程时间片,一个进程时间片结束执行下一个,多个进程穿插执行

2.有如下代码

public class Example implements Runnable{
	public void run(){
		while(true){
		}
	}
	
	public static void main(String[] args) {
		Example ex1 = new Example();
		Example ex2 = new Example();
		Example ex3 = new Example();
		ex1.run();
		ex2.run();
		ex3.run();
		
	}
}

在这里插入图片描述
答:C
3.有以下代码

class Example implements Runnable{
	public static void main(String[] args) {
		Thread t = new Thread(new Example());
		t.start();
	}
	
	public void run(int limit){
		for(int x =0; x<limit;x++){
			System.out.println(x);
		}
	}
}

在这里插入图片描述
答:C
4.有以下代码

class Example {
	public static void main(String[] args) {
		Thread.sleep(3000);
		System.out.println("sleep");
	}
}

在这里插入图片描述
答:A
5.
在这里插入图片描述
代码:

import java.util.Random;
public class TestPrint {
	public static void main(String[] args) {
		Number n = new Number();
		Thread l = new Thread(new Letter());
		n.start();
		l.start();
	}

}
//输出100个1~26
class Number extends Thread{
	public void run(){
		for(int i = 1; i<=100; i++){
			Random r = new Random();
			int a =r.nextInt(26)+1;
			System.out.println("数字"+a);
		}
	}
}
//输出100个A~Z
class Letter implements Runnable{
	public void run() {
		for(int i = 1; i<=100; i++){
//			Random r = new Random();
//			int a = r.nextInt(26)+65;
//			char c = (char)a;
			
			char c = (char) ( new Random().nextInt(26)+65 );
			System.out.println("            字母    "+c);
		}
	}
}

运行结果
在这里插入图片描述
6.

public class TestMyThread {
	public static void main(String[] args) {
		Object lock = new Object();
		Thread t1 = new MyThread1(lock);
		Thread t2 = new MyThread2(lock);
		t1.start();
		t2.start();
	}
}

class MyThread1 extends Thread {
	Object lock;

	public MyThread1(Object lock) {
		this.lock = lock;
	}

	public void run() {
		synchronized (lock) {// 1
			for (int i = 0; i <= 10; i++) {
				try {
					Thread.sleep((int) (Math.random() * 1000));
				} catch (Exception e) {
				}
				System.out.println("$$$");
			}
		}
	}
}

class MyThread2 extends Thread {
	Object lock;

	public MyThread2(Object lock) {
		this.lock = lock;
	}

	public void run() {
		synchronized (lock) {// 2
			for (int i = 0; i <= 10; i++) {
				try {
					Thread.sleep((int) (Math.random() * 1000));
				} catch (Exception e) {

				}
				System.out.println("###");
			}
		}
	}
}

问:在//1和//2处加上的synchronized起什么作用?如果不加synchronized,运行程序有什么不同的地方
对临界资源对象加锁,保证每一个进程的原子操作不被破坏;
如果不加的话,会出现两个程序并发访问临界资源,导致原子结构被破坏有可能出现数据异常
7.

public class TestMyThread {
	public static void main(String[] args) {
		Thread t1 = new MyThread("hello");
		Thread t2 = new MyThread("world");
		t1.start();
		t2.start();
	}
}

class MyThread extends Thread{
	private String data;
	
	public MyThread(String data) {
		super();
		this.data = data;
	}

	public void run(){
		synchronized (data) {
			for(int i=0; i<10 ;i++){
				try {
					Thread.sleep((int) (Math.random()*1000));
				} catch (Exception e) {
				}
				System.out.println(data);
			}
		}
	}
}

在这里插入图片描述
选C

发布了24 篇原创文章 · 获赞 1 · 访问量 719

猜你喜欢

转载自blog.csdn.net/weixin_46286064/article/details/104804617
今日推荐