选择题
1.A 2.B 3.C 4.B 5.C 6.C
简答题
1.单CPU的计算机中,CPU同时只能执行一条指令,所以在仅有一个CPU的计算机上不可能同时执行多个任务。而操作系统为了能提高程序的运行效率,将CPU的执行时间分成多个时间片,分配给不同的线程,当一个时间片执行完毕后,该线程就可能让出CPU使用权限交付给下一个时间片的其他线程执行。
2.线程实例调用start()方法和调用run()方法有着天壤之别,前者是调用实例方法,只有主线程执行,只有一条执行路径,后者是启动线程。
3.任何线程一般都具有5种状态,即创建、就绪、运行、阻塞、终止状态。
1>创建状态
在程序中用构造方法创建了一个线程对象后,新的线程对象就处于创建状态,此时,它已经获取了相应的资源,但还没有处于可运行状态,这时可以通过Thread类的方法来设置线程对象的属性,如设置线程名(setName())、设置线程优先级(setPriority())等。
2>就绪状态
线程创建之后,就可以通过调用start()方法启动线程,即进入就绪状态。
3>运行状态
当就绪状态的线程获得CPU资源时,即可转入运行状态,执行的run()方法。对于只有一个CPU的机器而言,任何时刻只能有一个处于运行状态的线程占用CPU,即获得CPU资源。
4>阻塞状态
一个正在运行的线程因某种原因不能继承运行时,进入阻塞状态。阻塞状态是一种“不可运行”的状态,而处于这种状态的线程在得到一个特定的事件之后会转回可运行状态。
可能使线程暂停执行的条件:
(1)线程优先级比较低,因此它不能获得CPU资源。
(2)使用sleep()方法使线程休眠。
(3)通过调用wait()方法,使线程等待。
(4)通过调用yield()方法,线程显式出让CPU控制权。
(5)线程由于等待一个文件I/O事件被阻塞。
5>死亡状态
一个线程的run()方法运行完毕,线程则进入死亡状态。处于死亡状态的线程不具有继承运行的能力。
public class New{
public static void main(String[] args) {
Mythread thread1 = new Mythread();
thread1.start();
}
}
class Mythread extends Thread{
public void run(){
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
public class New2 {
public static void main(String[] args) {
Thread thread1 = new Thread(new Mythread2());
thread1.start();
}
}
class Mythread2 implements Runnable{
public void run(){
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
public class Account {
private int balance=500;
public int getBalance() {
return balance;
}
public void withdraw(int amount) {
balance=balance-amount;
}
}
public class TestAccount implements Runnable {
private Account acct = new Account();
public void run() {
for (int i = 0; i < 5; i++) {
makeWithdrawal(100);
if (acct.getBalance() < 0) {
System.out.println("账户透支了!");
}
}
}
private synchronized void makeWithdrawal(int amt) {
if (acct.getBalance() >= amt) {
System.out.println(Thread.currentThread().getName() + " 准备取款");
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
}
acct.withdraw(amt);
System.out.println(Thread.currentThread().getName() + " 完成取款");
} else {
System.out.println("余额不足以支付" + Thread.currentThread().getName()
+ " 的取款,余额为 " + acct.getBalance());
}
}
}
public class TestWithdrawal {
public static void main(String[] args) {
TestAccount r=new TestAccount();
Thread one=new Thread(r);
Thread two=new Thread(r);
one.setName("张三");
two.setName("张三的妻子");
one.start();
two.start();
}
}
练习
public class CureThread implements Runnable {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("VIP:"+(i+1)+"号病人在看病!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
Thread thread = new Thread(new CureThread());
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
for(int i=0;i<20;i++){
System.out.println("Ordinary:"+(i+1)+"号病人在看病!");
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
if(i==9){
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
练习
public class Test {
public static void main(String[] args) {
Thread thread1 = new Thread(new Mythread());
Thread thread2 = new Thread(new Mythread());
thread2.start();
thread1.start();
}
}
class Mythread implements Runnable{
public void run(){
for(int i=1;i<=20;i++){
System.out.println(i+".你好,来自线程"+Thread.currentThread().getName());
}
}
}
练习
public class Site implements Runnable {
private int count = 10;
private int num = 0;
private boolean flag = false;
public void run() {
while (true) {
if(!sale()){
break;
}
}
}
public synchronized boolean sale() {
if (count <= 0) {
return false;
}
num++;
count--;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "抢到第" + num
+ "张票,剩余" + count + "张票!");
if(Thread.currentThread().getName().equals("黄牛党")){
return false;
}
return true;
}
}
public class Test {
public static void main(String[] args) {
Site site = new Site();
Thread person1= new Thread(site,"桃跑跑");
Thread person2= new Thread(site,"抢票代理");
Thread person3= new Thread(site,"黄牛党");
person1.start();
person2.start();
person3.start();
}
}
博客包含选择题答案,还介绍了单CPU计算机中操作系统提高程序运行效率的方式,即把CPU执行时间分成时间片分配给不同线程。同时阐述了线程调用start()和run()方法的区别,以及线程的创建、就绪、运行、阻塞、终止5种状态及转换条件。
1857

被折叠的 条评论
为什么被折叠?



