Why does this code always print nothing after run for a while? [duplicate]
This question already has an answer here:
Loop doesn't see value changed by other thread without a print statement
1 answer
I run the main method after a while, it will always print nothing.
But if I uncomment "System.out.println(111);" and "System.out.println(222);",it will run normal i think.
I fetch the dump of 'soldWorker' thread and 'refundWorker' thread, they stop at the if
judge.
public class ProducerAndConsumer2 {
public static void main(String args) {
SyncTikcet2 syncTikcet = new SyncTikcet2(1);
Runnable syncSoldTicket = () -> syncTikcet.soldTicket();
Runnable syncRefundTicket = () -> syncTikcet.refund();
new Thread(syncSoldTicket, "soldWorker").start();
new Thread(syncRefundTicket, "refundWorker").start();
}
}
class SyncTikcet2 {
private Integer ticketNum;
public SyncTikcet2(Integer ticketNum) {
this.ticketNum = ticketNum;
}
public void soldTicket() {
while (true) {
if (ticketNum > 0) {
ticketNum = ticketNum - 1;
System.out.println("sold ticket,the num is" + ticketNum);
} else {
// System.out.println(111);
}
}
}
public void refund() {
while (true) {
if (ticketNum == 0) {
ticketNum = ticketNum + 1;
System.out.println("refund ticket,the num is" + ticketNum);
} else {
// System.out.println(222);
}
}
}
public Integer getTicketNum() {
return ticketNum;
}
public void setTicketNum(Integer ticketNum) {
this.ticketNum = ticketNum;
}
}
java multithreading
marked as duplicate by Arnaud, khelwood
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 8:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Loop doesn't see value changed by other thread without a print statement
1 answer
I run the main method after a while, it will always print nothing.
But if I uncomment "System.out.println(111);" and "System.out.println(222);",it will run normal i think.
I fetch the dump of 'soldWorker' thread and 'refundWorker' thread, they stop at the if
judge.
public class ProducerAndConsumer2 {
public static void main(String args) {
SyncTikcet2 syncTikcet = new SyncTikcet2(1);
Runnable syncSoldTicket = () -> syncTikcet.soldTicket();
Runnable syncRefundTicket = () -> syncTikcet.refund();
new Thread(syncSoldTicket, "soldWorker").start();
new Thread(syncRefundTicket, "refundWorker").start();
}
}
class SyncTikcet2 {
private Integer ticketNum;
public SyncTikcet2(Integer ticketNum) {
this.ticketNum = ticketNum;
}
public void soldTicket() {
while (true) {
if (ticketNum > 0) {
ticketNum = ticketNum - 1;
System.out.println("sold ticket,the num is" + ticketNum);
} else {
// System.out.println(111);
}
}
}
public void refund() {
while (true) {
if (ticketNum == 0) {
ticketNum = ticketNum + 1;
System.out.println("refund ticket,the num is" + ticketNum);
} else {
// System.out.println(222);
}
}
}
public Integer getTicketNum() {
return ticketNum;
}
public void setTicketNum(Integer ticketNum) {
this.ticketNum = ticketNum;
}
}
java multithreading
marked as duplicate by Arnaud, khelwood
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 8:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Loop doesn't see value changed by other thread without a print statement
1 answer
I run the main method after a while, it will always print nothing.
But if I uncomment "System.out.println(111);" and "System.out.println(222);",it will run normal i think.
I fetch the dump of 'soldWorker' thread and 'refundWorker' thread, they stop at the if
judge.
public class ProducerAndConsumer2 {
public static void main(String args) {
SyncTikcet2 syncTikcet = new SyncTikcet2(1);
Runnable syncSoldTicket = () -> syncTikcet.soldTicket();
Runnable syncRefundTicket = () -> syncTikcet.refund();
new Thread(syncSoldTicket, "soldWorker").start();
new Thread(syncRefundTicket, "refundWorker").start();
}
}
class SyncTikcet2 {
private Integer ticketNum;
public SyncTikcet2(Integer ticketNum) {
this.ticketNum = ticketNum;
}
public void soldTicket() {
while (true) {
if (ticketNum > 0) {
ticketNum = ticketNum - 1;
System.out.println("sold ticket,the num is" + ticketNum);
} else {
// System.out.println(111);
}
}
}
public void refund() {
while (true) {
if (ticketNum == 0) {
ticketNum = ticketNum + 1;
System.out.println("refund ticket,the num is" + ticketNum);
} else {
// System.out.println(222);
}
}
}
public Integer getTicketNum() {
return ticketNum;
}
public void setTicketNum(Integer ticketNum) {
this.ticketNum = ticketNum;
}
}
java multithreading
This question already has an answer here:
Loop doesn't see value changed by other thread without a print statement
1 answer
I run the main method after a while, it will always print nothing.
But if I uncomment "System.out.println(111);" and "System.out.println(222);",it will run normal i think.
I fetch the dump of 'soldWorker' thread and 'refundWorker' thread, they stop at the if
judge.
public class ProducerAndConsumer2 {
public static void main(String args) {
SyncTikcet2 syncTikcet = new SyncTikcet2(1);
Runnable syncSoldTicket = () -> syncTikcet.soldTicket();
Runnable syncRefundTicket = () -> syncTikcet.refund();
new Thread(syncSoldTicket, "soldWorker").start();
new Thread(syncRefundTicket, "refundWorker").start();
}
}
class SyncTikcet2 {
private Integer ticketNum;
public SyncTikcet2(Integer ticketNum) {
this.ticketNum = ticketNum;
}
public void soldTicket() {
while (true) {
if (ticketNum > 0) {
ticketNum = ticketNum - 1;
System.out.println("sold ticket,the num is" + ticketNum);
} else {
// System.out.println(111);
}
}
}
public void refund() {
while (true) {
if (ticketNum == 0) {
ticketNum = ticketNum + 1;
System.out.println("refund ticket,the num is" + ticketNum);
} else {
// System.out.println(222);
}
}
}
public Integer getTicketNum() {
return ticketNum;
}
public void setTicketNum(Integer ticketNum) {
this.ticketNum = ticketNum;
}
}
This question already has an answer here:
Loop doesn't see value changed by other thread without a print statement
1 answer
java multithreading
java multithreading
asked Nov 14 '18 at 8:34
wr1ttenyu zhaowr1ttenyu zhao
163
163
marked as duplicate by Arnaud, khelwood
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 8:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Arnaud, khelwood
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 8:46
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If you haven't made the field volatile, there is no reason for a thread to re-read a value it didn't change.
Try using private volatile int ticketNum
I suggest using a primitive e.g. int
, unless you have to use an Object reference. It's not only faster but it also makes it clear you don't expect the value to be null
.
thank you very much, you are so great, your answer guide me to find another question link. This question is the same as mine and it's answer more detailed. Thank you very much once again.
– wr1ttenyu zhao
Nov 14 '18 at 9:05
1
@wr1ttenyuzhao note System.out.println has asynchronized
memory barrier, however if it's not called you might not see an update.
– Peter Lawrey
Nov 14 '18 at 9:14
yeah, this is why it will run normal when i uncomment "System.out.println(111);" and "System.out.println(222);", thank your answer, you are a good man, thanks
– wr1ttenyu zhao
Nov 14 '18 at 9:59
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you haven't made the field volatile, there is no reason for a thread to re-read a value it didn't change.
Try using private volatile int ticketNum
I suggest using a primitive e.g. int
, unless you have to use an Object reference. It's not only faster but it also makes it clear you don't expect the value to be null
.
thank you very much, you are so great, your answer guide me to find another question link. This question is the same as mine and it's answer more detailed. Thank you very much once again.
– wr1ttenyu zhao
Nov 14 '18 at 9:05
1
@wr1ttenyuzhao note System.out.println has asynchronized
memory barrier, however if it's not called you might not see an update.
– Peter Lawrey
Nov 14 '18 at 9:14
yeah, this is why it will run normal when i uncomment "System.out.println(111);" and "System.out.println(222);", thank your answer, you are a good man, thanks
– wr1ttenyu zhao
Nov 14 '18 at 9:59
add a comment |
If you haven't made the field volatile, there is no reason for a thread to re-read a value it didn't change.
Try using private volatile int ticketNum
I suggest using a primitive e.g. int
, unless you have to use an Object reference. It's not only faster but it also makes it clear you don't expect the value to be null
.
thank you very much, you are so great, your answer guide me to find another question link. This question is the same as mine and it's answer more detailed. Thank you very much once again.
– wr1ttenyu zhao
Nov 14 '18 at 9:05
1
@wr1ttenyuzhao note System.out.println has asynchronized
memory barrier, however if it's not called you might not see an update.
– Peter Lawrey
Nov 14 '18 at 9:14
yeah, this is why it will run normal when i uncomment "System.out.println(111);" and "System.out.println(222);", thank your answer, you are a good man, thanks
– wr1ttenyu zhao
Nov 14 '18 at 9:59
add a comment |
If you haven't made the field volatile, there is no reason for a thread to re-read a value it didn't change.
Try using private volatile int ticketNum
I suggest using a primitive e.g. int
, unless you have to use an Object reference. It's not only faster but it also makes it clear you don't expect the value to be null
.
If you haven't made the field volatile, there is no reason for a thread to re-read a value it didn't change.
Try using private volatile int ticketNum
I suggest using a primitive e.g. int
, unless you have to use an Object reference. It's not only faster but it also makes it clear you don't expect the value to be null
.
answered Nov 14 '18 at 8:38
Peter LawreyPeter Lawrey
443k56565967
443k56565967
thank you very much, you are so great, your answer guide me to find another question link. This question is the same as mine and it's answer more detailed. Thank you very much once again.
– wr1ttenyu zhao
Nov 14 '18 at 9:05
1
@wr1ttenyuzhao note System.out.println has asynchronized
memory barrier, however if it's not called you might not see an update.
– Peter Lawrey
Nov 14 '18 at 9:14
yeah, this is why it will run normal when i uncomment "System.out.println(111);" and "System.out.println(222);", thank your answer, you are a good man, thanks
– wr1ttenyu zhao
Nov 14 '18 at 9:59
add a comment |
thank you very much, you are so great, your answer guide me to find another question link. This question is the same as mine and it's answer more detailed. Thank you very much once again.
– wr1ttenyu zhao
Nov 14 '18 at 9:05
1
@wr1ttenyuzhao note System.out.println has asynchronized
memory barrier, however if it's not called you might not see an update.
– Peter Lawrey
Nov 14 '18 at 9:14
yeah, this is why it will run normal when i uncomment "System.out.println(111);" and "System.out.println(222);", thank your answer, you are a good man, thanks
– wr1ttenyu zhao
Nov 14 '18 at 9:59
thank you very much, you are so great, your answer guide me to find another question link. This question is the same as mine and it's answer more detailed. Thank you very much once again.
– wr1ttenyu zhao
Nov 14 '18 at 9:05
thank you very much, you are so great, your answer guide me to find another question link. This question is the same as mine and it's answer more detailed. Thank you very much once again.
– wr1ttenyu zhao
Nov 14 '18 at 9:05
1
1
@wr1ttenyuzhao note System.out.println has a
synchronized
memory barrier, however if it's not called you might not see an update.– Peter Lawrey
Nov 14 '18 at 9:14
@wr1ttenyuzhao note System.out.println has a
synchronized
memory barrier, however if it's not called you might not see an update.– Peter Lawrey
Nov 14 '18 at 9:14
yeah, this is why it will run normal when i uncomment "System.out.println(111);" and "System.out.println(222);", thank your answer, you are a good man, thanks
– wr1ttenyu zhao
Nov 14 '18 at 9:59
yeah, this is why it will run normal when i uncomment "System.out.println(111);" and "System.out.println(222);", thank your answer, you are a good man, thanks
– wr1ttenyu zhao
Nov 14 '18 at 9:59
add a comment |