201871010133- Zhao Yongjun, "object-oriented programming (java)" Week 17 learning summary

201871010133- Zhao Yongjun, "object-oriented programming (java)" Week 17 learning summary

project content
This work belongs courses https://www.cnblogs.com/nwnu-daizh/
Where this requirement in the job https://www.cnblogs.com/nwnu-daizh/p/12073034.html
Job learning objectives

(1) understand and master the property and priority scheduling method thread;

(2) to grasp the concept and implementation of thread synchronization technology;

(3) Java threads comprehensive programming exercises

 

 

 

 

 

 

 

 

 

 

 

Part I: theoretical part

Question 1, multi-threaded execution

◆ relative order of execution of multiple threads is uncertain.

◆ thread execution order uncertainty create uncertainty in the results.

◆ often generate this uncertainty in a multi-threaded operations on shared data.

2, thread synchronization

- Multiple threads run concurrently uncertainties solution: the introduction of thread synchronization mechanism, so that another thread To use this method, you can only wait.

-  way to solve the problem of multi-thread synchronization in Java, there are two:

- Java SE 5.0 class introduced ReentrantLock

- plus synchronized modifier in front of the class method of shared memory.

……

public synchronized static void sub(int m)

……

(1) Solution a: lock object and target conditions

The basic structure of the protective ReentrantLock code block with the following:

myLock.lock();

try {

     critical section

}

finally

{

     myLock.unlock(); 

}

The key points about the lock object and condition object:

➢ to protect the lock code segments, to ensure that any time only one thread is executing code in protected.

➢ Lock Manager thread attempting to enter the protected code segment.

➢ lock may have one or more conditions related objects.
➢ Each condition object management thread that has entered the protected code segment but can not run.

(2) Solution two: synchronized keyword

the role of the synchronized keyword:

➢某个类内方法用synchronized 修饰后,该方法被称为同步方法;
➢只要某个线程正在访问同步方法,其他线程欲要访问同步方法就被阻塞,直至线程从同步方法返回前唤醒被阻塞线程,其他线程方可能进入同步方法。

3、在同步方法中使用wait()、notify 和notifyAll()方法

➢ 一个线程在使用的同步方法中时,可能根据问题的需要,必须使用wait()方法使本线程等待,暂时让出CPU的使用权,并允许其它线程使用这个同步方法。

➢ 线程如果用完同步方法,应当执行notifyAll()方法通知所有由于使用这个同步方法而处于等待的线程结束等待。

 

第二部分:实验部分

1、实验目的与要求

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

(2) 线程综合编程练习

2、实验内容和步骤

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

测试程序1:

※在Elipse环境下调试教材651页程序14-7,结合程序运行结果理解程序;

※掌握利用锁对象和条件对象实现的多线程同步技术

实验程序如下:

 1 package synch;
 2 
 3 import java.util.*;
 4 import java.util.concurrent.locks.*;
 5 
 6 /**
 7 一个银行有许多银行帐户,使用锁序列化访问 * @version 1.30 2004-08-01
 8  * @author Cay Horstmann
 9  */
10 public class Bank
11 {
12    private final double[] accounts;
13    private Lock bankLock;
14    private Condition sufficientFunds;
15 
16    /**
17     * 建设银行。
18     * @param n 账号
19     * @param initialBalance 每个账户的初始余额
20     */
21    public Bank(int n, double initialBalance)
22    {
23       accounts = new double[n];
24       Arrays.fill(accounts, initialBalance);
25       bankLock = new ReentrantLock();
26       sufficientFunds = bankLock.newCondition();
27    }
28 
29    /**
30     * 把钱从一个账户转到另一个账户。
31     * @param 从账户转账
32     * @param 转到要转账的账户
33     * @param 请允许我向你转达
34     */
35    public void transfer(int from, int to, double amount) throws InterruptedException
36    {
37       bankLock.lock();
38       try
39       {
40          while (accounts[from] < amount)
41             sufficientFunds.await();
42          System.out.print(Thread.currentThread());
43          accounts[from] -= amount;
44          System.out.printf(" %10.2f from %d to %d", amount, from, to);
45          accounts[to] += amount;
46          System.out.printf(" Total Balance: %10.2f%n", getTotalBalance());
47          sufficientFunds.signalAll();
48       }
49       finally
50       {
51          bankLock.unlock();
52       }
53    }
54 
55    /**
56     * 获取所有帐户余额的总和。
57     * @return 总余额
58     */
59    public double getTotalBalance()
60    {
61       bankLock.lock();
62       try
63       {
64          double sum = 0;
65 
66          for (double a : accounts)
67             sum += a;
68 
69          return sum;
70       }
71       finally
72       {
73          bankLock.unlock();
74       }
75    }
76 
77    /**
78     * 获取银行中的帐户数量。
79     * @return 账号
80     */
81    public int size()
82    {
83       return accounts.length;
84    }
85 }
View Code
 1 package synch;
 2 
 3 /**
 4  * 这个程序显示了多个线程如何安全地访问数据结构。
 5  * @version 1.31 2015-06-21
 6  * @author Cay Horstmann
 7  */
 8 public class SynchBankTest
 9 {
10    public static final int NACCOUNTS = 100;
11    public static final double INITIAL_BALANCE = 1000;
12    public static final double MAX_AMOUNT = 1000;
13    public static final int DELAY = 10;
14    
15    public static void main(String[] args)
16    {
17       Bank bank = new Bank(NACCOUNTS, INITIAL_BALANCE);
18       for (int i = 0; i < NACCOUNTS; i++)
19       {
20          int fromAccount = i;
21          Runnable r = () -> {
22             try
23             {
24                while (true)
25                {
26                   int toAccount = (int) (bank.size() * Math.random());
27                   double amount = MAX_AMOUNT * Math.random();
28                   bank.transfer(fromAccount, toAccount, amount);
29                   Thread.sleep((int) (DELAY * Math.random()));
30                }
31             }
32             catch (InterruptedException e)
33             {
34             }            
35          };
36          Thread t = new Thread(r);
37          t.start();
38       }
39    }
40 }
View Code

实验结果如下:

测试程序2:

※在Elipse环境下调试教材655页程序14-8,结合程序运行结果理解程序;

※掌握synchronized在多线程同步中的应用。

实验程序如下:

 1 package synch2;
 2 
 3 import java.util.*;
 4 
 5 /**
 6  * 具有多个使用同步原语的银行账户的银行。
 7  * @version 1.30 2004-08-01
 8  * @author Cay Horstmann
 9  */
10 public class Bank
11 {
12    private final double[] accounts;
13 
14    /**
15     * 建设银行。
16     * @param n 账号
17     * @param initialBalance 每个账户的初始余额
18     */
19    public Bank(int n, double initialBalance)
20    {
21       accounts = new double[n];
22       Arrays.fill(accounts, initialBalance);
23    }
24 
25    /**
26     * 把钱从一个账户转到另一个账户。
27     * @param 从账户转账
28     * @param 转到要转账的账户
29     * @param 请允许我向你转达
30     */
31    public synchronized void transfer(int from, int to, double amount) throws InterruptedException
32    {
33       while (accounts[from] < amount)
34          wait();
35       System.out.print(Thread.currentThread());
36       accounts[from] -= amount;
37       System.out.printf(" %10.2f from %d to %d", amount, from, to);
38       accounts[to] += amount;
39       System.out.printf(" Total Balance: %10.2f%n", getTotalBalance());
40       notifyAll();
41    }
42 
43    /**
44     * 获取所有帐户余额的总和。
45     * @return 总余额
46     */
47    public synchronized double getTotalBalance()
48    {
49       double sum = 0;
50 
51       for (double a : accounts)
52          sum += a;
53 
54       return sum;
55    }
56 
57    /**
58     * 获取银行中的帐户数量。
59     * @return 
60     */
61    public int size()
62    {
63       return accounts.length;
64    }
65 }
View Code
 1 package synch2;
 2 
 3 /**
 4  * 
 5  * 这个程序展示了多个线程如何使用同步方法安全地访问数据结构。
 6  * @version 1.31 2015-06-21
 7  * @author Cay Horstmann
 8  */
 9 public class SynchBankTest2
10 {
11    public static final int NACCOUNTS = 100;
12    public static final double INITIAL_BALANCE = 1000;
13    public static final double MAX_AMOUNT = 1000;
14    public static final int DELAY = 10;
15 
16    public static void main(String[] args)
17    {
18       Bank bank = new Bank(NACCOUNTS, INITIAL_BALANCE);
19       for (int i = 0; i < NACCOUNTS; i++)
20       {
21          int fromAccount = i;
22          Runnable r = () -> {
23             try
24             {
25                while (true)
26                {
27                   int toAccount = (int) (bank.size() * Math.random());
28                   double amount = MAX_AMOUNT * Math.random();
29                   bank.transfer(fromAccount, toAccount, amount);
30                   Thread.sleep((int) (DELAY * Math.random()));
31                }
32             }
33             catch (InterruptedException e)
34             {
35             }
36          };
37          Thread t = new Thread(r);
38          t.start();
39       }
40    }
41 }
View Code

实验结果如下:

测试程序3:

※在Elipse环境下运行以下程序,结合程序运行结果分析程序存在问题;

※尝试解决程序中存在问题。

class Cbank

{

     private static int s=2000;

     public   static void sub(int m)

     {

           int temp=s;

           temp=temp-m;

          try {

     Thread.sleep((int)(1000*Math.random()));

   }

           catch (InterruptedException e)  {              }

          s=temp;

          System.out.println("s="+s);

  }

}

class Customer extends Thread

{

  public void run()

  {

   for( int i=1; i<=4; i++)

     Cbank.sub(100);

    }

 }

public class Thread3

{

 public static void main(String args[])

  {

   Customer customer1 = new Customer();

   Customer customer2 = new Customer();

   customer1.start();

   customer2.start();

  }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

实验程序如下:

 1 package test2;
 2 class Cbank
 3 {
 4      private static int s=2000;
 5      public  static void sub(int m)
 6      {
 7            int temp=s;
 8            temp=temp-m;
 9           try {
10                  Thread.sleep((int)(1000*Math.random()));
11                }
12            catch (InterruptedException e)  {              }
13               s=temp;
14               System.out.println("s="+s);
15           }
16     }
17 
18 
19 class Customer extends Thread
20 {
21   public void run()
22   {
23    for( int i=1; i<=4; i++)
24      Cbank.sub(100);
25     }
26  }
27 public class Thread3
28 {
29  public static void main(String args[])
30   {
31    Customer customer1 = new Customer();
32    Customer customer2 = new Customer();
33    customer1.start();
34    customer2.start();
35   }
36 }
View Code

实验结果如下:

实验2  编程练习

利用多线程及同步方法,编写一个程序模拟火车票售票系统,共3个窗口,卖10张票,程序输出结果类似(程序输出不唯一,可以是其他类似结果)。

Thread-0窗口售:第1张票

Thread-0窗口售:第2张票

Thread-1窗口售:第3张票

Thread-2窗口售:第4张票

Thread-2窗口售:第5张票

Thread-1窗口售:第6张票

Thread-0窗口售:第7张票

Thread-2窗口售:第8张票

Thread-1窗口售:第9张票

Thread-0窗口售:第10张票

程序设计思路简述:

    程序的主要设计思路没有太繁琐,只是新建三个售票口,在thread类中创建线程并开启线程,然后在run方法中定义线程任务,进行异常处理后设计在10张票数内时将售票情况进行打印,票数超过10张时程序结束。

实验程序如下:

 1 public class Demo {
 2     public static void main(String[] args) {
 3         Mythread mythread = new Mythread();
 4         Thread ticket1 = new Thread(mythread);
 5         Thread ticket2 = new Thread(mythread);
 6         Thread ticket3 = new Thread(mythread);
 7         ticket1.start();
 8         ticket2.start();
 9         ticket3.start();
10     }
11 }
12 
13 class Mythread implements Runnable {
14     int ticket = 1;
15     boolean flag = true;
16 
17     @Override
18     public void run() {
19         while (flag) {
20             try {
21                 Thread.sleep(500);
22             } catch (InterruptedException e) {
23                 // TODO Auto-generated catch block
24                 e.printStackTrace();
25             }
26 
27             synchronized (this) {
28                 if (ticket <= 10) {
29                     System.out.println(Thread.currentThread().getName() + "窗口售:第" + ticket + "张票");
30                     ticket++;
31                 }
32                 if (ticket > 10) {
33                     flag = false;
34                 }
35             }
36 
37         }
38     }
39 
40 }
View Code

实验结果如下:

 

结对过程描述及结对照片:

 第三部分:实验总结

       这周继续学习了有关线程的知识,主要学习了有关线程同步的问题。线程同步主要是为了解决多线程并发运行不确定性问题,使得多个线程中在一个线程使用某种方法时候,另一线程要使用该方法,就只能等待。实验课上,通过自己运行实验代码理解了多线程中在不加锁时会出现的情况,让我们对线程同步有了更加清晰地认识。虽然很多地方还是不太懂还是存在很大的问题,通过看书和网上公开课后才得以理解。

Guess you like

Origin www.cnblogs.com/zhaoyongjun0911/p/12084269.html