Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java use Thread and Runnable threads

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report