[英]Thread Safety and code synchronization in java
I was reading about Thread Safety and code synchronization in java .. I did this code in java . 我正在阅读有关Java中的线程安全和代码同步的信息。 I do not like its output when it shows true or false in the output .
当输出中显示true或false时,我不喜欢它的输出。
do you have any idea to remove the true and false from the output to improve my code performance ? 您是否有想法从输出中删除是非,以提高我的代码性能?
does my code works according to the Thread Safety and synchronization ? 我的代码是否根据线程安全和同步工作?
thanks 谢谢
public class B {
public int numberOfTotalTickets=2;
public int numberT;
public String nameP;
public boolean check(int numberOfTickets, String name) {
numberT=numberOfTickets;
nameP=name;
if(numberOfTickets <=numberOfTotalTickets) {
actionOk(nameP);
return true;
} else {
actionNotOk(nameP);
return false;
}
}
public void actionOk(String nameP) {
numberOfTotalTickets=numberOfTotalTickets-numberT;
System.out.println("your booking is complete"+" "+ nameP);
}
public void actionNotOk(String nameP) {
System.out.println("number of tickets available"+" "+ numberOfTotalTickets);
System.out.println("sorry"+" "+ nameP +" " +"your booking is not complete because we have only"+" "+numberOfTotalTickets+" "+"tickets");
}
public static void main(String[] args) {
B b = new B();
System.out.println(b.check(2, "jack"));
System.out.println(b.check(2, "sam"));
}
}
my code's output is 我的代码的输出是
your booking is complete jack
true
number of tickets available 0
sorry sam your booking is not complete because we have only 0 tickets
false
This code is not thread-safe. 此代码不是线程安全的。
public void actionOk(String nameP) {
numberOfTotalTickets=numberOfTotalTickets-numberT;
System.out.println("your booking is complete"+" "+ nameP);
}
If two threads are accessing the instance variables at the same time, then the wrong number of tickets could be returned. 如果两个线程同时访问实例变量,则可能返回错误数量的票证。
Consider this 考虑一下
Thread1: read number of tickets as 2
Thread1: interrupted
Thread2: read number of tickets as 2
Thread2: assigns 2 tickets
Thread1: resumes and also assigns 2 tickets.
Both threads would see that 2 tickets are available. 两个线程都会看到有2张票。
Regarding printing of true/false: You are printing the output of check
function, because it is type of boolean
and you call it inside System.out.println
. 关于true / false的打印:您正在打印
check
函数的输出,因为它是boolean
类型,并且在System.out.println
内部调用它。 Just call it directly, without System.out.println
. 无需
System.out.println
即可直接调用它。
Regarding Thread Safety: Try to use private
access modifiers when not requiring access from other classes. 关于线程安全性:当不需要其他类的访问时,请尝试使用
private
访问修饰符。 If I understand correctly what you want to achieve with this code (you just need to call check
from outside), you can transform everything to private
apart from your check
method and by adding the synchronized
keyword to this method, you are safe. 如果我正确理解了您希望使用此代码实现的目标(您只需要从外部调用
check
),则可以将除check
方法之外的所有内容都转换为private
,并通过向此方法添加synchronized
关键字来保证安全。
public synchronized boolean check(int numberOfTickets, String name) {
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.