Running IDT algorithm related environment Configure Ubuntu14.04+OpenCV-2.4.9+FFMPEG-2.4.3jane

2023-01-23   ES  

(1)

Step:

1. Define the implementation of the Runnable interface

2. Cover the Run method in the Runnable interface and store the code to be running threads in the RUN method.

3. Establish a thread object through the Thread class.

4. Pass the subclass objects of the Runnable interface as the constructor of the actual parameter to the Thread class.

Why do you want to talk about the constructor transmitted to Thread by the subclass objects of the Runnable interface. Because the object of the custom method is the sub -class object of the Runnable interface.

5. Call the start method of the Thread class to open a thread and call the Runnable interface class Run method.

(2) Thread -sharing code block problem

Purpose: Is there a security problem in the program, how to solve it if so?

How to find a problem:

1. Defined which code is multi -threaded running code.

2. Clearly shared data

3. Determine which sentences in multi -threaded running code are operating shared data.

class Bank{

	private int sum;
	public void add(int n){
	
	     sum+=n;
		 System.out.println("sum="+sum);
	
	}

}
 class Cus implements Runnable{

   private Bank b=new Bank();
   public void run(){
     synchronized(b){   
		 for(int x=0;x<3;x++)
		 {
			b.add(100);
		 
		 }
	 }
   }

}
public class BankDemo{
    public static void main(String []args){
	    Cus c=new Cus();
		Thread t1=new Thread(c);
		Thread t2=new Thread(c);
		t1.start();
		t2.start();
	
	
	}


}
or the second method, place synchronous code synchronized in the modification method.
class Bank{

	private int sum;
	public synchronized void add(int n){
	    Object obj=new Object();
	     
	      sum+=n;
		 try{
		      Thread.sleep(10);
		 }catch(Exception e){
		   e.printStackTrace();
		 }
		 System.out.println("sum="+sum);
	    
	}

}
 class Cus implements Runnable{

   private Bank b=new Bank();
   public void run(){
       
		 for(int x=0;x<3;x++)
		 {
			b.add(100);
		 
		 }
	 
   }

}
public class BankDemo{
    public static void main(String []args){
	    Cus c=new Cus();
		Thread t1=new Thread(c);
		Thread t2=new Thread(c);
		t1.start();
		t2.start();
	
	
	}


}

Summary:

1. Define the problems to deal with in a class, method.

2. Rewilling the RUN method in the implementation of the Runnable class to call the method of dealing with the defined classes.

Accept the object of the problem to deal with the problem in the synchronized block.

3. Define multiple threads to execute in the main method.

source

Related Posts

[Design mode Learning note] Filter mode

and iOS development talk about jailbreak

1 1 1 1 1 1 1 1 LOI

mysql distinct pay attention to yqq

Running IDT algorithm related environment Configure Ubuntu14.04+OpenCV-2.4.9+FFMPEG-2.4.3jane

Random Posts

In IDEA, the YML, the Properties file created by myself in the SpringBoot project is not automatically prompted

Googleads unity plugin Chinese tutorial latest version

Shandong Polytechnic OJ 2052 Paper Makeup Angel

YOLO3 training, testing your own data set

JENKINS Local Publisher Sweet SHE