Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Java use Thread and Runnable threads

Shulou Source: shulou.com Published: 2022-06-03 09:20:56 10月04日 Update

This article mainly shows you "Java how to use Thread and Runnable threads", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Java how to use Thread and Runnable threads" this article.

The details are as follows:

Using Thread to realize multi-thread simulation of railway ticketing system

1 code

Public class ThreadDemo {public static void main (String [] args) {TestThread newTh = new TestThread (); / / A thread object can only start newTh.start ();}} class TestThread extends Thread {private int tickets = 5; public void run () {while (tickets > 0) {System.out.println (Thread.currentThread (). GetName () + "ticket for sale" + tickets); tickets-= 1 }}}

2 run

Thread-0 ticket 5Thread-0 ticket 4Thread-0 ticket 3Thread-0 ticket 2Thread-0 ticket 1Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start (Thread.java:708) at ThreadDemo.main (ThreadDemo.java:16)

3 description

A thread can only be started once

Four threads are generated in the two main method

1 code

Public class ThreadDemo {public static void main (String [] args) {/ / starts four threads to perform their respective operations new TestThread (). Start ();}} class TestThread extends Thread {private int tickets = 5 Public void run () {while (tickets > 0) {System.out.println (Thread.currentThread (). GetName () + "ticket for sale" + tickets); tickets-= 1;}

2 run

Thread-0 selling tickets 5Thread-0 selling tickets 4Thread-0 selling tickets 3Thread-0 selling tickets 2Thread-0 selling tickets 1Thread-1 selling tickets 5Thread-1 selling tickets 4Thread-1 selling tickets 3Thread-1 selling tickets 2Thread-1 selling tickets 1Thread-2 selling tickets 5Thread-2 selling tickets 3Thread-2 selling tickets 2Thread-2 selling tickets 1Thread-3 selling tickets 4Thread-3 selling tickets 3Thread-3 selling tickets 2Thread-3 selling tickets 1

Third, use Runnable interface to realize multithreading and resource sharing.

1 code

Public class RunnableDemo {public static void main (String [] args) {TestThread newTh = new TestThread (); / / started four threads and achieved the purpose of resource sharing: new Thread (newTh). Start ();} class TestThread implements Runnable {private int tickets = 5 Public void run () {while (tickets > 0) {System.out.println (Thread.currentThread (). GetName () + "ticket for sale" + tickets); tickets-= 1;}

2 run

Thread-0 ticket 5Thread-0 ticket 4Thread-0 ticket 3Thread-0 ticket 2Thread-0 ticket 1

These are all the contents of the article "how Java uses Thread and Runnable threads". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Threads code content articles runs resources resource sharing learning help objects interfaces methods understandability more organization purpose knowledge systems ribbons industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology MariaDB macOS Redmi