我们知道线程在cpu上的使用权并不是长时间的,因为计算机的cpu只有一个,而在计算上运行的进程有很多,线程就更不用说了,所以cpu只能通过调度来上多个线程轮流占用cpu资源运行,且为了保障多个进程能够同时运行。cpu只能通过频繁调度线程来是的每个线程在几乎同一时间内都能得到cpu的使用权来执行代码来保证程序能够正常运行,所以cpu的调度频率是很快的。那么问题来了。如下代码:

 1 package cn.wz.traditional.wf;
 2 
 3 import javafx.scene.chart.PieChart;
 4 
 5 /**
 6  * Created by WangZhe on 2017/5/4.
 7  */
 8 public class ThraedLocalTest {
 9     public static void main(String[] args) {
10             new Thread(new Runnable() {
11                 public void run() {
12                     A.age=5;
13                    A.age+=10;
14                     System.out.println(A.age);
15                 }
16             }).start();
17 
18             new Thread(new Runnable() {
19                 public void run() {
20                    A.age=7;
21                 }
22             }).start();
23     }
24     static class A{
25         static Integer age=5;
26         private Integer data;
27         public A(Integer data){
28             this.data=data;
29         }
30         public void sayHi(){
31             data+=10;
32             System.out.println(data);
33 
34         }
35         public void sayHello(){
36            data=5;
37         }
38     }
39 }
demo

相关文章:

  • 2021-08-22
  • 2021-11-08
  • 2022-02-09
  • 2022-02-09
  • 2022-03-01
  • 2021-10-25
  • 2021-08-28
  • 2021-07-07
猜你喜欢
  • 2022-12-23
  • 2021-10-23
  • 2022-01-03
  • 2021-08-20
  • 2021-11-13
  • 2021-06-06
相关资源
相似解决方案