多线程的并发一般不是程序员决定,而是由容器决定

多线程的并发一般不是程序员决定,而是由容器决定。

多线程出现故障的原因:

两个线程同时访问一个数据资源(临界资源),形成数据发生不一致和不完整。

数据的不一致往往是因为一个线程中的两个关联的操作只完成了一步。

 1 package TomTexts;
 2 
 3 
 4 class ExceptionExam7_1 extends Exception        //自定义异常类ExceptionExam7_1
 5 {
 6 private int show;
 7 ExceptionExam7_1 (int a)
 8 {
 9 show=a;
10 }
11 public String toString()
12 {
13 return "ExceptionExam7_1 <"+show+">";
14 }
15 }
16 public class TomTexts_37
17 {
18 static void caculate(int a) throws ExceptionExam7_1
19 {
20 System.out.println("对["+ a +"]已经进行过相应的操作");
21 if(a>100)
22 throw new ExceptionExam7_1 (a);
23 System.out.println("执行该算法正常退出!");
24 }
25 public static void main(String args[])
26 {
27 try
28 {
29 caculate(1);
30 caculate(1000);
31 }
32 catch (ExceptionExam7_1 e)
33 {
34 System.out.println("捕获了异常" + e);
35 }
36 }
37 }

猜你喜欢

转载自www.cnblogs.com/borter/p/9425127.html