class Mythread extends Thread {
private int ticket = 5;
@Override
public void run() {
for(int i =0 ; i<10 ; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO auto-generated catch block e.printStackTrace(); } if(ticket > 0 ) { ticket--; L.d ("The number of remaining tickets is" + ticket + ""); } } } }
public class ThreadSample {
public static void main(String[] args) {
Mythread mythread = new Mythread();
L.d ("Ticket sales start"); mythread.start(); } }
result: ThreadSample # main: 34 thread name: main thread id: 1 gentest :: Ticket sales start Mythread # run: 24 thread name: Thread-0 thread id: 11 gentest :: 4 tickets left Mythread # run: 24 thread name: Thread-0 thread id: 11 gentest :: 3 tickets left Mythread # run: 24 thread name: Thread-0 thread id: 11 gentest :: 2 tickets left Mythread # run: 24 thread name: Thread-0 thread id: 11 gentest :: 1 ticket left Mythread # run: 24 thread name: Thread-0 thread id: 11 gentest :: 0 tickets left
interface MyTaskResult {
void ticketSoldout();
void ticketRemain();
}
class Mythread extends Thread {
private MyTaskResult myTaskResult;
public Mythread (MyTaskResult myTaskResult) {
this.myTaskResult = myTaskResult;
}
private int ticket = 5;
@Override
public void run() {
for(int i =0 ; i<10 ; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO auto-generated catch block e.printStackTrace(); } if(ticket > 0 ) { ticket--; L.d ("The number of remaining tickets is" + ticket + ""); } } if(ticket>0) { myTaskResult.ticketRemain(); }else { myTaskResult.ticketSoldout(); } } }
public class ThreadSample {
public static void main(String[] args) {
Mythread mythread = new Mythread(myTaskResult);
L.d ("Ticket sales start"); mythread.start(); } static MyTaskResult myTaskResult = new MyTaskResult() {
@Override
public void ticketSoldout() {
L.d ("Thank you for your hard work."); }
@Override
public void ticketRemain() {
L.d ("Isn't it over?"); } }; }
Result is
ThreadSample # main: 43 thread name: main thread id: 1 gentest :: Ticket sales start Mythread # run: 28 thread name: Thread-0 thread id: 11 gentest :: 4 tickets left Mythread # run: 28 thread name: Thread-0 thread id: 11 gentest :: 3 tickets left Mythread # run: 28 thread name: Thread-0 thread id: 11 gentest :: 2 tickets left Mythread # run: 28 thread name: Thread-0 thread id: 11 gentest :: 1 ticket left Mythread # run: 28 thread name: Thread-0 thread id: 11 gentest :: 0 tickets left ThreadSample $ 1 # ticketSoldout: 50 thread name: Thread-0 thread id: 11 gentest :: Thank you.
interface MyTaskResult {
void ticketSoldout(String threadName);
void ticketRemain(String threadName);
}
class TicketBox implements Runnable {
private int ticket = 5;
@Override
public void run() {
for(int i =0 ; i<10 ; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO auto-generated catch block e.printStackTrace(); } if(ticket > 0 ) { ticket--; L.d ("The number of remaining tickets is" + ticket + ""); } } L.d ("Completed ticket:" + ticket); } }
class Mythread extends Thread {
private MyTaskResult myTaskResult;
public Mythread (Runnable r, MyTaskResult myTaskResult,String threadName) {
super(r);
this.myTaskResult = myTaskResult;
setName(threadName);
}
private int ticket = 5;
@Override
public void run() {
super.run();
}
}
public class ThreadSample {
public static void main(String[] args) {
TicketBox ticketBox = new TicketBox();
Mythread mythread1 = new Mythread (ticketBox, myTaskResult, "proxy 1"); Mythread mythread2 = new Mythread (ticketBox, myTaskResult, "proxy 2"); Mythread mythread3 = new Mythread (ticketBox, myTaskResult, "proxy 3"); L.d ("Ticket sales start"); mythread1.start(); mythread2.start(); mythread3.start(); } static MyTaskResult myTaskResult = new MyTaskResult() {
@Override
public void ticketSoldout(String threadName) {
L.d ("Thank you for your hard work." + ThreadName); }
@Override
public void ticketRemain(String threadName) {
L.d ("Isn't it over?" + ThreadName); } }; }
result
ThreadSample # main: 52 thread name: main thread id: 1 gentest :: Ticket sales start TicketBox # run: 23 thread name: proxy 2 thread id: 12 gentest :: 3 tickets left TicketBox # run: 23 thread name: proxy 1 thread id: 11 gentest :: 3 tickets left TicketBox # run: 23 thread name: proxy 3 thread id: 13 gentest :: 3 remaining tickets TicketBox # run: 23 thread name: proxy 1 thread id: 11 gentest :: 2 tickets left TicketBox # run: 23 thread name: proxy 2 thread id: 12 gentest :: 1 remaining ticket TicketBox # run: 23 thread name: proxy 3 thread id: 13 gentest :: 0 remaining tickets TicketBox # run: 26 thread name: Proxy 2 thread id: 12 gentest :: Completed remaining tickets: 0 TicketBox # run: 26 thread name: Proxy 3 thread id: 13 gentest :: Completed remaining tickets: 0 TicketBox # run: 26 thread name: Proxy 1 thread id: 11 gentest :: Completed remaining tickets: 0
problem:
class TicketBox implements Runnable {
private int ticket = 5;
@Override
public void run() {
for(int i =0 ; i<10 ; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO auto-generated catch block e.printStackTrace(); } saleTicket(); } L.d ("Completed remaining tickets:" + ticket); } private synchronized void saleTicket() { if(ticket > 0 ) { ticket--; L.d ("The number of remaining tickets is" + ticket + ""); } } }
result
ThreadSample # main: 55 thread name: main thread id: 1 gentest :: Ticket sales start TicketBox # saleTicket: 28 thread name: Proxy 2 thread id: 12 gentest :: 4 tickets left TicketBox # saleTicket: 28 thread name: Proxy 3 thread id: 13 gentest :: 3 tickets left TicketBox # saleTicket: 28 thread name: Proxy 1 thread id: 11 gentest :: 2 tickets left TicketBox # saleTicket: 28 thread name: Proxy 2 thread id: 12 gentest :: 1 remaining ticket TicketBox # saleTicket: 28 thread name: Proxy 1 thread id: 11 gentest :: 0 remaining tickets TicketBox # run: 23 thread name: Proxy 2 thread id: 12 gentest :: Completed remaining tickets: 0 TicketBox # run: 23 thread name: Proxy 3 thread id: 13 gentest :: Completed remaining tickets: 0 TicketBox # run: 23 thread name: Proxy 1 thread id: 11 gentest :: Completed remaining tickets: 0
result
ThreadSample # main: 85 thread name: main thread id: 1 gentest :: Ticket sales start TicketBox # saleTicket: 52 thread name: Proxy 2 thread id: 12 gentest :: 4 tickets left TicketBox # saleTicket: 52 thread name: Proxy 1 thread id: 11 gentest :: 3 tickets left TicketBox # saleTicket: 52 thread name: Proxy 3 thread id: 13 gentest :: 2 tickets left TicketBox # saleTicket: 52 thread name: Proxy 1 thread id: 11 gentest :: 1 remaining ticket TicketBox # saleTicket: 52 thread name: Proxy 2 thread id: 12 gentest :: 0 tickets left ThreadSample $ 1 # taskDone: 95 thread name: Proxy 1 thread id: 11 gentest :: Thank you. Deputy 1 ThreadSample $ 1 # taskDone: 95 thread name: Proxy 2 thread id: 12 gentest :: Thank you. Surrogate 2 ThreadSample $ 1 # taskDone: 95 thread name: Proxy 3 thread id: 13 gentest :: Thank you. Deputy 3 ThreadSample $ 1 # taskDone: 100 thread name: Proxy 3 thread id: 13 gentest :: ---------------- All completed report ------------- --------------- ThreadSample $ 2 # sendResult: 108 thread name: Proxy 3 thread id: 13 gentest :: Total number of tickets: 5 ThreadSample $ 2 # sendResult: 109 thread name: Proxy 3 thread id: 13 gentest :: Remaining tickets: 0 ThreadSample $ 2 # sendResult: 110 thread name: Proxy 3 thread id: 13 gentest :: Proxy 1 Sales result: 2 ThreadSample $ 2 # sendResult: 111 thread name: Proxy 3 thread id: 13 gentest :: Proxy 2 Sales result: 2 ThreadSample $ 2 # sendResult: 112 thread name: Proxy 3 thread id: 13 gentest :: Proxy 3 Sales Result: 1
Recommended Posts