kurento to build video server and DEMO demonstration

2023-03-16  

1. Pack the shared data in another object, and then pass this object to each Runnable object one by one. Each thread’s operation method of shared data is also allocated on that object to complete, so that it is easy to realize the mutual exclusion and communication of each operation of the data.
2. Use these Runnable objects as internal classes in a certain class, sharing data as member variables in this external class, and each thread is also assigned to the external class to the shared data to achieve the shared data. The mutual exclusion and communications of each operation are used as the method of calling the external class as each Runnable object in the internal class.
The combination of the above two methods: encapsulate the shared data in another object, and the operation method of the shared data on each thread is also assigned to the object. Local variables, the Runnable object of each thread is the internal or part of the internal class of members in the external class.
In short, it is best to simultaneously placed in several independent methods. These methods are placed in the same class, which is easier to achieve synchronization and communication between them.
Tags:<No>

code fragment(2)[View all code in full screen]

1. [File] ThreadShareDataTest.java ~ 1KB     download(13)     

01 package cn.itcast.gz;
02  
03 public class ThreadShareDataTest {
04       
05     public static void main(String[] args) {
06          
07         ShareData shareData = new ShareData();
08          
09         Add add = new Add(shareData);
10         Sub sub = new Sub(shareData);
11          
12         Thread t1 = new Thread(add);
13         Thread t2 = new Thread(add);
14         Thread t3 = new Thread(sub);
15         Thread t4 = new Thread(sub);
16          
17         t1.start();
18         t2.start();
19         t3.start();
20         t4.start();
21     }
22 }
23  
24  
25 class ShareData
26 {
27     private Integer j=100;
28      
29     public synchronized void add()
30     {
31         j++;
32         System.out.println(Thread.currentThread().getName()+"Putting an additional operation to J"+j);
33     }
34      
35     public synchronized  void sub()
36     {
37         j--;
38         System.out.println(Thread.currentThread().getName()+"Ulipping the operation of J"+j);
39     }
40 }
41  
42  
43 class Add implements Runnable
44 {
45  
46     private ShareData data;
47     public Add(ShareData data)
48     {
49         this.data = data;
50     }
51     @Override
52     public void run() {
53         data.add();
54     }
55 }
56  
57 class Sub implements Runnable
58 {
59     private ShareData data;
60     public Sub(ShareData data)
61     {
62         this.data = data;
63     }
64     @Override
65     public void run() {
66          
67         data.sub();
68     }
69 }

2. [File] ThreadShareDataTest2.java ~ 853B     download(12)     jump to [1] [2] [full screen preview]

01 package cn.itcast.gz;
02  
03 public class ThreadShareDataTest2 {
04  
05      public static void main(String[] args) {
06          
07           
08          final ShareData2 shareData = new ShareData2();
09           
10          for(int i = 0;i<2;i++)
11          {
12              new Thread()
13              {
14                  @Override
15                 public void run() {
16                      shareData.add();
17                 }
18              }.start();
19          }
20           
21           
22          for(int i = 0;i<2;i++)
23          {
24              new Thread()
25              {
26                  @Override
27                 public void run() {
28                      shareData.sub();
29                  }
30              }.start();
31          }
32           
33     }
34 }
35  
36 class ShareData2
37 {
38     private Integer j = 100;
39     public  synchronized void add()
40     {
41         j++;
42         System.out.println(Thread.currentThread().getName()+"Pay the addition of J to J"+j);
43     }
44      
45     public  synchronized void sub()
46     {
47         j--;
48         System.out.println(Thread.currentThread().getName()+"Perform j subtraction operations"+j);
49     }
50 }

source

Related Posts

linux virtual network device tun/tap

Black Horse Programmer -Foundation framework NSFileManager introduction and usage

python2.7 and 3.5 dual versions coexist and the use of PIP

Output month English name (20 points)

kurento to build video server and DEMO demonstration

Random Posts

Business operation and maintenance actual combat: How does Tencent optimize the APP user experience?

WIN32 program to activate the window of the previous startup program during startup

mysql database security backup

(HDU 5774) WHERE AMAZING HAPENS

SQLDIAG-Configuration File-Extension