千锋Java&Day28课后作业

线程的创建,同步

1.一个单 CPU 的机器,如何同时执行多个线程?请简述其原理。

单CPU中进程只能是并发,多CPU计算机中进程可以并行。
单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();
      }
 }
 选择正确答案:
 A.代码编译失败,因为 ex2.run()无法获得执行
 B.代码编译成功,存在 3 个可运行的线程
 C.代码编译成功,存在 1 个可运行的线程
B

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);
         }
     }
}
选择正确答案:
A.打印输出,从 0 至 limit
B.无内容输出,因为没有明确调用 run()方法。
C.代码编译失败,因为没有正确实现 Runnable 接口
D.代码编译失败,如果声明类为抽象类,可使代码编译成功。
E.代码编译失败,如果去掉 implements Runnable,可使代码编译成功。
C

4.(sleep 方法)有如下代码

class Example {
     public static void main(String args[]) { 
        Thread.sleep(3000); 
        System.out.println(“sleep”);
     }
}
选择正确答案:
A.编译出错
B.运行时异常
C.正常编译运行,输出 sleep
D.正常编译运行,但没有内容输出
A

5.(线程的创建)创建两个线程

要求如下
1)一个线程输出 1001~26,另一个线程输出 100 个 A~Z
2)一个线程使用继承 Thread 类的写法,另一个线程使用实现 Runnable 接口的写法。
输出100126:
public class ThreadOut {
     public static void main(String[] args) {
         Thread1 t = new Thread1();
         t.start();
      }
}
class Thread1 extends Thread {
       public void run() {
           for (int i = 0; i < 100; i++) {
              for (int j = 1; j <= 26; j++) {
                 System.out.println(j);
              }
           }
       }
}

输出100个A~Z:
public class ThreadOut {
     public static void main(String[] args) {
        Thread2 t2 = new Thread2();
        Thread t = new Thread(t2);
        t.start();
     }
}
class Thread2 implements Runnable {
      public void run() {
          for (int i = 0; i < 100; i++) {
            for (int j = 0; j <= 25; j++) {
                System.out.println((char)(j+65));
            }
          }
      }
}

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<=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(“###”);
             }
          }
       }
   }
   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
    处加上的 synchronized 起什么作用?
    如果不加 synchronized,运行程序有什么不同的地方?
同步块, 只允许一个线程进行访问内部代码,
如果不加的话, 运行程序将交互进行

7.(线程同步)有如下代码

class MyThread extends Thread{
     private String 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();
      }
 }
 问:上述代码输出的结果是什么?
 A.先输出 100 个 hello,然后是 100 个 world
 B.先输出 100 个 world,然后是 100 个 hello
 C.线程不同步,因此交替输出 hello 和 world
C
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值