千锋20200311

在千锋“逆战”学习第28天

      每日一句:为什么很多人都想成功,但成功的人只是极少数?不是因为成功有多困难,其实成功简单得很,只要你一开始都有计划,并且能一直做下去,总会成功的。关键是如何面对成功,你失败了,起码还有人同情你,而成功了,连得到同情的机会都没有。
      今天学习了线程的相关知识。
      明天继续加油。

作业

1.一个单CPU的机器,如何同时执行多个线程
      单核CPU同一时间只能处理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();
	}
}

运行结果:

代码编译失败,因为ex2.run()无法获得运行

--------------------------------------------------------------------------
3.有如下代码:

public 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);
		}
	}
}

运行结果:

代码编译失败,因为没有正确实现Runnable接口

--------------------------------------------------------------------------
4.有如下代码:

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

运行结果:

编译出错,没有捕捉异常

--------------------------------------------------------------------------
5.创建两个线程,要求如下
      1)一个线程输出100个1-26,另一个线程输出100个A-Z
      2)一个线程继承Thread类的写法,另一个线程实现Runnable接口的写法

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

//输出100个1-26
class MyThread1 extends Thread{
	public void run() {
		for(int i=1;i<=100;i++) {
			for(int j=1;j<=26;j++) {
				System.out.println(Thread.currentThread().getName()+"-"+i+"-"+j);
			}
		}
	}
}

//输出100个A-Z
class MyThread2 implements Runnable{
	public void run() {
		for(int i=1;i<=100;i++) {
			for(int j=65;j<=90;j++) {
				System.out.println(Thread.currentThread().getName()+"-"+i+"-"+(char)j);
			}
		}
	}
}

运行结果(部分):

Thread-0-1-1
Thread-1-1-A
Thread-0-1-2
Thread-0-1-3
Thread-0-1-4
Thread-0-1-5
Thread-0-1-6
···
Thread-1-52-G
Thread-1-52-H
Thread-1-52-I
Thread-1-52-J
Thread-1-52-K
Thread-1-52-L
Thread-1-52-M
Thread-1-52-N
···
Thread-0-100-17
Thread-0-100-18
Thread-0-100-19
Thread-0-100-20
Thread-0-100-21
Thread-0-100-22
Thread-0-100-23
Thread-0-100-24
Thread-0-100-25
Thread-0-100-26

--------------------------------------------------------------------------
6.有如下代码:

class MyThread1 extends Thread {
	Object lock;

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

	public void run() {
		synchronized (lock) {// 1
			for (int i = 0; i <= 100; 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) {// 1
			for (int i = 0; i <= 100; i++) {
				try {
					Thread.sleep((int) (Math.random() * 1000));
				} catch (Exception e) {
				}
				System.out.println("###");
			}
		}
	}
}

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();
	}
}

在//1、//2处加上的synchorized起什么作用?如果不加,运行程序有什么不同。
答:synchorized使线程更加安全。如果线程1先运行,那么线程2必须等待线程1运行结束后才能运行。如果不加,线程1和线程2,会同时抢占CPU资源,输出结果不一定连续。

--------------------------------------------------------------------------
7.有如下代码:

class MyThread extends Thread {
	private String data;
public MyThread(String data) {
		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);
			}
		}
	}
}

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

运行结果:

线程不同步,交替输出Hello和World
发布了40 篇原创文章 · 获赞 0 · 访问量 1141

猜你喜欢

转载自blog.csdn.net/qq_41841482/article/details/104804513
今日推荐