As a Java web Developer, there is little or no need to process threads because the server has handled it for us. Remember freshman just learned Java, the teacher took us to do a LAN chat room, with the AWT, Socket, multi-threading, I/O, write the client and server, at that time to do out very excited, back to school to the students to demonstrate, feel good NB, hehe, pull away. The last time in the Baidu developer conference to see a hint, their own code, 6 months do not look at other people's code, their knowledge is the same, learned knowledge if not used or not often reviewed, then not their own knowledge. University fragmented for less than four years of Java, I believe a lot of people are like me, Javase Foundation did not fight, hurried busy, excitedly engaged in Java EE, and then learn the foreground development (HTML, CSS, JavaScript), may also engage in jquery, ExtJS , then struts, Hibernate, Spring, and then heard to find a job will be Linux, Oracle, and to learn, in the process, whether lost, although the learning surface is very broad, but like the "God of the Eagle" in the Yellow Pharmacist evaluation Yang, unfocused, miscellaneous and impure, this string down, Feel that Java development is difficult, not learning difficult, but the knowledge is too broad, but also proficient in this, but also proficient in that, this is just my lost time of the idea, now I have found the direction.
Back to the point, when we look at the JDK API, we always find some class instructions that say, thread-safe or thread-insecure, such as StringBuilder, that " StringBuilder it's not safe to use instances for multiple threads." If such synchronization is required, it is recommended StringBuffer . ", then manually create a thread-unsafe class, and then use this class in multi-threading to see what the effect is.
Count.java:
[Java]View PlainCopyprint?
- Public class Count {
- private int num;
- public Void Count () {
- For (int i = 1; I <= ; i++) {
- num + = i;
- }
- System.out.println (Thread.CurrentThread (). GetName () + "-" + num);
- }
- }
The Count method in this class is to calculate the sum of 1 to 10 and output the current thread name and sum, and we expect that each thread will output 55.
Threadtest.java:
[Java]View PlainCopyprint?
- Public class ThreadTest {
- public static void Main (string[] args) {
- Runnable Runnable = new Runnable () {
- Count count = new count ();
- public Void Run () {
- Count.count ();
- }
- };
- For (int i = 0; i < i++) {
- new Thread (runnable). Start ();
- }
- }
- }
This launches 10 threads and looks at the output:
[Java]View PlainCopy print?
- 0-thread-
- thread-1-
- thread-2-165
- thread-4- -
- thread-5-275
- 6-thread-
- thread-3-385
- 7-thread-
- thread-8-495
- thread-9-550
Only the results of the Thread-0 thread output are what we expect, and the output is cumulative each time, here the reasons for the accumulation of the post will be explained, then to get the results we expect, there are several solutions:
1. Change num in count to the local variable of the Count method;
[Java]View PlainCopyprint?
- Public class Count {
- public Void Count () {
- int num = 0;
- For (int i = 1; I <= ; i++) {
- num + = i;
- }
- System.out.println (Thread.CurrentThread (). GetName () + "-" + num);
- }
- }
2. Take the thread class member variable to the Run method, when the count reference is a local variable within the thread;
[Java]View PlainCopyprint?
- Public class ThreadTest4 {
- public static void Main (string[] args) {
- Runnable Runnable = new Runnable () {
- public Void Run () {
- Count count = new count ();
- Count.count ();
- }
- };
- For (int i = 0; i < i++) {
- new Thread (runnable). Start ();
- }
- }
- }
3. Each time a thread is started with a different thread class, it is not recommended.
The above tests, we found that the existence of member variables of the class used for multi-threading is unsafe, unsafe reflected in this member variable may occur non-atomic operations , and variables are defined in the method that the local variables are thread-safe. When using STRUTS1, it is not recommended to create member variables, because action is a singleton, and if you create a member variable, there is a risk of thread insecurity, and struts2 is creating an action for each request, regardless of the thread safety issue. Therefore, in the daily development, it is usually necessary to consider the member variables or global variables in the multi-threaded environment, whether it will cause some problems.
This article from: Gao | Coder, original address: http://blog.csdn.net/ghsau/article/details/7421217
Summary: http://blog.csdn.net/ghsau/article/details/17609747
Look at the clouds: http://www.kancloud.cn/digest/java-thread/107455
Java Thread (i): Thread safety and insecurity