[Java] Thread and Runnable

Step 1, Program assuming ticket sales

1.1 Create Mythread

1.2 Start from main function

Below is a sample

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

Step 2, Add report (callback) after ticket sales are completed

2.1 Create interface

2.2 Realization of interface in main class

2.3 Added Mythread constructor

2.4 Add callback to Mythread run

Below is a sample

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.

Step 3, sell tickets at multiple ticket machines

3.1 Addition of Runnable (TicketBox)

3.2 Addition of number of Threads, addition of Thread name (proxy 1,2,3)

Below is a sample

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:

  1. I sold 6 times for 5 tickets. (Synchronization required)
  2. Each agency does not stat how many copies have been released

Step 4 Synchronous processing

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

Step 5 Summary of sales results

Please refer to github

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

[Java] Thread and Runnable
Java Programming Thread Runnable
Java thread processing
Java and JavaScript
XXE and Java
Java true and false
[Java] String comparison and && and ||
Java --Serialization and Deserialization
[Java] Arguments and parameters
Thread safe summary ~ Java ~
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
Study Java # 2 (\ mark and operator)
Java version 8 and later features
[Java] New Thread generation method (2)
[Java] Difference between == and equals
[Java] Stack area and static area
[Java] Generics classes and generics methods
Java programming (variables and data)
Java encryption and decryption PDF
Java and Iterator Part 1 External Iterator
Java if and switch statements
Java class definition and instantiation
Apache Hadoop and Java 9 (Part 1)
[Java] About String and StringBuilder
[Java] HashCode and equals overrides
☾ Java / Iterative statement and iterative control statement
Java thread safe for you
Java methods and method overloads
java Generics T and? Difference
Advantages and disadvantages of Java
java (conditional branching and repetition)
About Java Packages and imports
[Java] Upload images and base64
Java thread to understand loosely
C # and Java Overrides Story
Java abstract methods and classes
Java while and for statements
Java encapsulation and getters and setters
[Java] New Thread generation method (1)
Implement Thread in Java and try using anonymous class, lambda
I compared PHP and Java constructors
Differences between "beginner" Java and Kotlin
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
[Java] Difference between Hashmap and HashTable
Java variable declaration, initialization, and types
Java Excel Insertion and Image Extraction
Install Java and Tomcat with Ansible
AWS SDK for Java 1.11.x and 2.x
[Java] Basic types and instruction notes
Java release date and EOL summary
Java and first-class functions-beyond functional interfaces-
About fastqc of Biocontainers and Java
Java for beginners, expressions and operators 1
Java Primer Series (Variables and Types)
Encoding and Decoding example in Java
[Java beginner] About abstraction and interface