Java basics-multithreading-② multithreading security issues, java

Source: Internet
Author: User

Java basics-multithreading-② multithreading security issues, java

What is thread security?

Previous Java basics-multithreading-① thread creation and startup we will use the Runnable interface to create a thread, and multiple threads can share resources:

1 class Dog implements Runnable {2 // define thread shared data 3 private int t = 100; 4 5 @ Override 6 public void run () {7 // TODO: endless loop, 8 while (true) {9 if (t> 0) {10 11 System. out. println ("current Thread:" + Thread. currentThread (). getName () + "---" + t --); 12} 13} 14} 15} 16 17 public class ThreadDemo {18 public static void main (String [] args) {19 System. out. println ("current Thread:" + Thread. currentThread (). getName (); 20 Dog dog = new Dog (); 21 22 Thread thread = new Thread (dog); 23 Thread thread2 = new Thread (dog); 24 thread. start (); 25 thread2.start (); 26 27} 28}

Start two threads, share data t = 100, and run the code in the run method: when t is greater than 0, print t --. Analyze possible problems:

Due to the uncertainty of fast switching of CPU time slices, this issue may not occur. to simulate this problem, before the execution of the print statement, let the thread sleep for 0.1 seconds:

1 @ Override 2 public void run () {3 while (true) {4 if (t> 0) {5 try {6 Thread. sleep (100); 7 System. out. println ("current Thread:" + Thread. currentThread (). getName () + "---" + t --); 8} catch (InterruptedException e) {9 // TODO: do not handle exceptions for now 10} 11} 12} 13}

 

Cause of the problem

The first reason is that the two threads share the data t. If multiple threads do not involve data sharing and execute their own code, this problem will not occur. This problem is involved in the thread-unsafe Singleton mode.

① Multiple threads share data.

If we do not judge t> 0, print t -- directly, or only judge t> 0, do not execute t --, as long as the loop does not end, the program does not terminate, logically speaking, no program Problems

② Design operations on shared data in a thread task (the operations here include ① determining t> 0; ② executing t --). When a thread is operating on shared data, other threads also operate on shared data.

This may cause data errors.

To sum up, when multiple threads execute the same piece of code, each execution result is the same as that of a single thread, and there is no ambiguity of the execution result, it can be called thread security. Thread security issues are mostly caused by global variables and static variables. When multiple threads only perform read operations on shared data and do not perform write operations, it is generally thread-safe; when multiple threads perform write operations, thread synchronization needs to be considered to solve thread security issues.


Thread security issues in java multithreading (Java)

Whether it is a class method or an instance method, when it is called, the local variables in the method are allocated memory space. After the method is called, the local variables immediately release the occupied memory.
According to the Java Virtual Machine specification, multiple instances share a method block in the memory. The methods are stored in the code segment, and multiple objects share a code space.
As long as this method does not hold class variables, instance variables, or other public resources, there is no concurrency synchronization problem!
At the same time, as mentioned in "deep into java Virtual Machine version 2", when a thread calls a java method, the virtual machine will put a new frame in the java stack of the thread. The new frame naturally becomes the current frame. When this method is executed, it uses this frame to store parameters, local variables, intermediate calculation results, and other data. That is to say, when multiple threads call the same method, their parameters are pushed into the stack frame, but only one stack frame is the current frame, so there is no problem you are worried about.

Java multi-thread security

I hope you are studying multithreading, not solving problems. If the problem is solved, we should use a class like ArrayBlockingQueue to implement identical functions.

In fact, your program has no problem with multithreading. It is similar to the example on the Java Condition document page. But you have also done this in the out () function. name = name + "... "+ count ++;, this statement changes the item name during each consumption! If the retrieved item is 808 and the count value is 808, the name is this. the name is 808, and the result count is 809, and the item name is 808... 809. so it looks like two items are taken out.

Delete this statement.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.