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 automatisch generierter Fangblock e.printStackTrace(); } if(ticket > 0 ) { ticket--; L.d ("Die Anzahl der verbleibenden Tickets ist" + Ticket + ""); } } } }
public class ThreadSample {
public static void main(String[] args) {
Mythread mythread = new Mythread();
L.d ("Ticketverkaufsstart"); mythread.start(); } }
Ergebnis: ThreadSample # main: 34 Threadname: main thread id: 1 gentest :: Ticketverkaufsstart Mythread # run: 24 Threadname: Thread-0 Thread-ID: 11 Gentest :: 4 Tickets übrig Mythread # run: 24 Threadname: Thread-0 Thread-ID: 11 Gentest :: 3 Tickets übrig Mythread # run: 24 Threadname: Thread-0 Thread-ID: 11 Gentest :: 2 Tickets übrig Mythread # run: 24 Threadname: Thread-0 Thread-ID: 11 Gentest :: 1 Ticket übrig Mythread # run: 24 Threadname: Thread-0 Thread-ID: 11 Gentest :: 0 Tickets übrig
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 automatisch generierter Fangblock e.printStackTrace(); } if(ticket > 0 ) { ticket--; L.d ("Die Anzahl der verbleibenden Tickets ist" + 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 ("Ticketverkaufsstart"); mythread.start(); } static MyTaskResult myTaskResult = new MyTaskResult() {
@Override
public void ticketSoldout() {
L.d ("Danke für Ihre harte Arbeit."); }
@Override
public void ticketRemain() {
L.d ("Ist es nicht vorbei?"); } }; }
Ergebnis ist
ThreadSample # main: 43 Threadname: main thread id: 1 gentest :: Ticketverkaufsstart Mythread # run: 28 Threadname: Thread-0 Thread-ID: 11 Gentest :: 4 Tickets übrig Mythread # run: 28 Threadname: Thread-0 Thread-ID: 11 Gentest :: 3 Tickets übrig Mythread # run: 28 Threadname: Thread-0 Thread-ID: 11 Gentest :: 2 Tickets übrig Mythread # run: 28 Threadname: Thread-0 Thread-ID: 11 Gentest :: 1 Ticket übrig Mythread # run: 28 Threadname: Thread-0 Thread-ID: 11 Gentest :: 0 Tickets übrig ThreadSample $ 1 # ticketSoldout: 50 Threadname: Thread-0 Thread-ID: 11 Gentest :: Danke.
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 automatisch generierter Fangblock e.printStackTrace(); } if(ticket > 0 ) { ticket--; L.d ("Die Anzahl der verbleibenden Tickets ist" + Ticket + ""); } } L.d ("Abgeschlossenes 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 = neuer Mythread (ticketBox, myTaskResult, "Proxy 1"); Mythread mythread2 = neuer Mythread (ticketBox, myTaskResult, "Proxy 2"); Mythread mythread3 = neuer Mythread (ticketBox, myTaskResult, "Proxy 3"); L.d ("Ticketverkaufsstart"); mythread1.start(); mythread2.start(); mythread3.start(); } static MyTaskResult myTaskResult = new MyTaskResult() {
@Override
public void ticketSoldout(String threadName) {
L.d ("Danke für deine harte Arbeit." + ThreadName); }
@Override
public void ticketRemain(String threadName) {
L.d ("Ist es nicht vorbei?" + ThreadName); } }; }
Ergebnis
ThreadSample # main: 52 Threadname: main thread id: 1 gentest :: Ticketverkaufsstart TicketBox # run: 23 Threadname: Proxy 2 Thread-ID: 12 Gentest :: 3 Tickets übrig TicketBox # run: 23 Threadname: Proxy 1 Thread-ID: 11 Gentest :: 3 Tickets übrig TicketBox # run: 23 Threadname: Proxy 3 Thread-ID: 13 Gentest :: 3 Tickets übrig TicketBox # run: 23 Threadname: Proxy 1 Thread-ID: 11 Gentest :: 2 Tickets übrig TicketBox # run: 23 Threadname: Proxy 2 Thread-ID: 12 Gentest :: 1 verbleibendes Ticket TicketBox # run: 23 Threadname: Proxy 3 Thread-ID: 13 Gentest :: 0 verbleibende Tickets TicketBox # run: 26 Threadname: Proxy 2 Thread-ID: 12 Gentest :: Abgeschlossene verbleibende Tickets: 0 TicketBox # run: 26 Threadname: Proxy 3 Thread-ID: 13 Gentest :: Abgeschlossene verbleibende Tickets: 0 TicketBox # run: 26 Threadname: Proxy 1 Thread-ID: 11 Gentest :: Abgeschlossene verbleibende 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 automatisch generierter Fangblock e.printStackTrace(); } saleTicket(); } L.d ("Restliches Ticket abgeschlossen:" + Ticket); } private synchronized void saleTicket() { if(ticket > 0 ) { ticket--; L.d ("Die Anzahl der verbleibenden Tickets ist" + Ticket + ""); } } }
Ergebnis
ThreadSample # main: 55 Threadname: main thread id: 1 gentest :: Ticketverkaufsstart TicketBox # saleTicket: 28 Thread-Name: Proxy 2 Thread-ID: 12 Gentest :: 4 Tickets übrig TicketBox # saleTicket: 28 Threadname: Proxy 3 Thread-ID: 13 Gentest :: 3 Tickets übrig TicketBox # saleTicket: 28 Thread-Name: Proxy 1 Thread-ID: 11 Gentest :: 2 Tickets übrig TicketBox # saleTicket: 28 Thread-Name: Proxy 2 Thread-ID: 12 Gentest :: 1 verbleibendes Ticket TicketBox # saleTicket: 28 Thread-Name: Proxy 1 Thread-ID: 11 Gentest :: 0 Tickets übrig TicketBox # run: 23 Threadname: Proxy 2 Thread-ID: 12 Gentest :: Abgeschlossene verbleibende Tickets: 0 TicketBox # run: 23 Threadname: Proxy 3 Thread-ID: 13 gentest :: Restliche Tickets abgeschlossen: 0 TicketBox # run: 23 Threadname: Proxy 1 Thread-ID: 11 gentest :: Restliche Tickets abgeschlossen: 0
Ergebnis
ThreadSample # main: 85 Threadname: main thread id: 1 gentest :: Ticketverkaufsstart TicketBox # saleTicket: 52 Threadname: Proxy 2 Thread-ID: 12 Gentest :: 4 Tickets übrig TicketBox # saleTicket: 52 Threadname: Proxy 1 Thread-ID: 11 Gentest :: 3 Tickets übrig TicketBox # saleTicket: 52 Threadname: Proxy 3 Thread-ID: 13 Gentest :: 2 Tickets übrig TicketBox # saleTicket: 52 Threadname: Proxy 1 Thread-ID: 11 Gentest :: 1 Ticket übrig TicketBox # saleTicket: 52 Threadname: Proxy 2 Thread-ID: 12 Gentest :: 0 Tickets übrig ThreadSample $ 1 # taskDone: 95 Threadname: Proxy 1 Thread-ID: 11 Gentest :: Danke. Stellvertreter 1 ThreadSample $ 1 # taskDone: 95 Threadname: Proxy 2 Thread-ID: 12 Gentest :: Danke. Ersatz 2 ThreadSample $ 1 # taskDone: 95 Threadname: Proxy 3 Thread-ID: 13 Gentest :: Vielen Dank für Ihre Arbeit. Stellvertreter 3 ThreadSample $ 1 # taskDone: 100 Threadname: Proxy 3 Thread-ID: 13 Gentest :: ---------------- Alle abgeschlossenen Berichte ------------- --------------- ThreadSample $ 2 # sendResult: 108 Threadname: Proxy 3 Thread-ID: 13 Gentest :: Gesamtzahl der Tickets: 5 ThreadSample $ 2 # sendResult: 109 Threadname: Proxy 3 Thread-ID: 13 Gentest :: Verbleibende Tickets: 0 ThreadSample $ 2 # sendResult: 110 Threadname: Proxy 3 Thread-ID: 13 Gentest :: Proxy 1 Verkaufsergebnis: 2 ThreadSample $ 2 # sendResult: 111 Threadname: Proxy 3 Thread-ID: 13 Gentest :: Proxy 2 Verkaufsergebnis: 2 ThreadSample $ 2 # sendResult: 112 Threadname: Proxy 3 Thread-ID: 13 Gentest :: Proxy 3 Verkaufsergebnis: 1
Recommended Posts