201771010125王瑜《面向对象程序设计(java) 》第十六周学习总结

                      201771010125王瑜《面向对象程序设计(java) 》第十六周学习总结

一 理论知识

1.程序是一段静态的代码;它是应用程序执行的蓝本。

    进程是程序的一次动态执行,它对应了从代码加载、执行至执行完毕的一个完整过程。

2.多线程是进程执行过程中产生的多条执行线索,县城不能独立存在,必须存在于进程中,同一进程的各线程间共享进程空间的数据

3.Java实现多线程有两种途径:

 (1)创建Thread类的子类:

      -定义Thread类的子类并实现用户线程操作,即 run()方法的实现;在适当的时候启动线程,由于Java只支持单重继承,用这种方法定义的类不可再继承其他父类。

 (2)在程序中定义实现Runnable接口的类:

        -首先设计一个实现Runnable接口的类,然后在类中根据需要重写run方法,再创建该类对象,以此对象为参数建立Thread 类的对象;调用Thread类对象的start方法启动线程,将 CPU执行权转交到run方法

4.线程阻塞:

(1)等待阻塞:通过调用线程的wait()方法,让线程等待某工作的完成。

(2)同步阻塞:线程在获取synchronized同步锁失败(因为锁被其它线程所占用),它会进入同步阻塞状态。

  (3)其他阻塞:通过调用线程的sleep()或join() 或发出了I/O请求时,线程会进入到阻塞状态。

5.线程的状态

    -利用各线程的状态变换,可以控制各个线程轮流使用CPU,体现多线程的并行性特征。 

    -线程有如下7种状态:

    New (新建)

    Runnable (可运行)

    Running(运行)

    Blocked (被阻塞)

    Waiting (等待)

    Timed waiting (计时等待) 

    Terminated (被终止)

6.多线程调度

    -Java 的线程调度采用优先级策略:

        优先级高的先执行,优先级低的后执行;

        多线程系统会自动为每个线程分配一个优先级,缺省时,继承其父类的优先级;

        任务紧急的线程,其优先级较高;

        同优先级的线程按“先进先出”的队列原则。

二 实验部分

1、实验目的与要求

(1) 掌握线程概念;

(2) 掌握线程创建的两种技术;

(3) 理解和掌握线程的优先级属性及调度方法;

(4) 掌握线程同步的概念及实现技术;

2、实验内容和步骤

实验1:测试程序并进行代码注释。

测试程序1:

l 在elipse IDE中调试运行ThreadTest,结合程序运行结果理解程序;

l 掌握线程概念;

l 掌握用Thread的扩展类实现线程的方法;

l 利用Runnable接口改造程序,掌握用Runnable接口创建线程的方法。

class Lefthand extends Thread { 

   public void run()

   {

       for(int i=0;i<=5;i++)

       {  System.out.println("You are Students!");

           try{   sleep(500);   }

           catch(InterruptedException e)

           { System.out.println("Lefthand error.");}    

       } 

  } 

}

class Righthand extends Thread {

    public void run()

    {

         for(int i=0;i<=5;i++)

         {   System.out.println("I am a Teacher!");

             try{  sleep(300);  }

             catch(InterruptedException e)

             { System.out.println("Righthand error.");}

         }

    }

}

public class ThreadTest 

{

     static Lefthand left;

     static Righthand right;

     public static void main(String[] args)

     {     left=new Lefthand();

           right=new Righthand();

           left.start();

           right.start();

     }

}

测试结果:

用runable接口实习:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Lefthand implements Runnable{
   public void run()
   {
       
       for(int i=0;i<=5;i++)
       {  System.out.println("You are Students!");
           try{ Thread.sleep(500);   }
           catch(InterruptedException e)
           { System.out.println("Lefthand error.");}   
       }
  }
}
class Righthand implements Runnable {
    public void run()
    {
         
          for(int i=0;i<=5;i++)
         {   System.out.println("I am a Teacher!");
             try{  Thread.sleep(300);  }
             catch(InterruptedException e)
             { System.out.println("Righthand error.");}
         }
    }
}
public class ThreadTest
{
     static Lefthand left;
     static Righthand right;
     public static void main(String[] args)
     {   
           Runnable lefthand = new Lefthand();
           Thread left=new Thread(lefthand);
            left.start();
            Runnable righthand = new Righthand();
            Thread right=new Thread(righthand);
              right.start();
               
     }
}

测试程序2:

l 在Elipse环境下调试教材625页程序14-1、14-2 、14-3,结合程序运行结果理解程序;

l 在Elipse环境下调试教材631页程序14-4,结合程序运行结果理解程序;

l 对比两个程序,理解线程的概念和用途;

l 掌握线程创建的两种技术。

测试程序3:分析以下程序运行结果并理解程序。

class Race extends Thread {

  public static void main(String args[]) {

    Race[] runner=new Race[4];

    for(int i=0;i<4;i++) runner[i]=new Race( );

   for(int i=0;i<4;i++) runner[i].start( );

   runner[1].setPriority(MIN_PRIORITY);

   runner[3].setPriority(MAX_PRIORITY);}

  public void run( ) {

      for(int i=0; i<1000000; i++);

      System.out.println(getName()+"线程的优先级是"+getPriority()+"已计算完毕!");

    }

}

 

测试程序4

l 教材642页程序模拟一个有若干账户的银行,随机地生成在这些账户之间转移钱款的交易。每一个账户有一个线程。在每一笔交易中,会从线程所服务的账户中随机转移一定数目的钱款到另一个随机账户。

l 在Elipse环境下调试教材642页程序14-5、14-6,结合程序运行结果理解程序;

 

  创建两个线程,每个线程按顺序输出5次“你好”,每个“你好”要标明来自哪个线程及其顺序号。

复制代码
package Project2;
class Lefthand extends Thread { 
   public void run()
   {
       for(int i=0;i<5;i++)
       {  System.out.println("你好 L");
           try{   sleep(500);   }
           catch(InterruptedException e)
           { System.out.println("Lefthand error.");}    
       } 
  } 
}
class Righthand extends Thread {
    public void run()
    {
         for(int i=0;i<5;i++)
         {   System.out.println("你好 R");
             try{  sleep(300);  }
             catch(InterruptedException e)
             { System.out.println("Righthand error.");}
         }
    }
}
public class ThreadTest 
{
     static Lefthand left;
     static Righthand right;
     public static void main(String[] args)
     {     left=new Lefthand();
           right=new Righthand();
           left.start();
           right.start();
     }
}
hello
复制代码

 三 实验总结:

  通过这次实验,学习了线程的有关知识,理解了实现多线程的两种途径:Thread子类和Runnable接口这两种方法,但在bank的实验中while循环产生随机数以及锁的概念不是很理解。希望老师可以再讲解一下

原文地址:https://www.cnblogs.com/wy-201771010125/p/10126839.html