7218

Java starting two threads? [duplicate]

<div class="question-status question-originals-of-duplicate">

This question already has an answer here:

    <li> What is the difference between Thread.start() and Thread.run()? <span class="question-originals-answer-count"> 9 answers </span> </li> </ul>

    i am new to java. I have two classes that looks like:

    public class hsClient implements Runnable { public void run() { while(true){ } } } public class hsServer implements Runnable { public void run() { while(true){ } } }

    If i try to start both classes as Thread it wont start the second thread. It looks like he stuck in the first one.

    This is my main class:

    public static void main(String[] args) throws IOException { hsClient client = new hsClient(); Thread tClient = new Thread(client); tClient.run(); System.out.println("Start Client"); hsServer server = new hsServer(); Thread tServer = new Thread(server); tServer.run(); System.out.println("Start Server"); }

    If i run my code it only prints "Start Client" but not "Start Server" on the console

    Answer1:

    Replace tClient.run() with tClient.start() and tServer.run() with tServer.start().

    Calling the run method directly executes it in the current thread instead of in a new thread.

    Answer2:

    To start a thread use the start method.

    Thread tClient = new Thread(client); tClient.start(); // start the thread

    More info on threads can be found e.g. in the JavaDoc

Recommend

  • Get current day's steps during datapointListener google Fit
  • VerifyInputWhenFocusTarget property has no effect
  • Implementing java gui login using multithreading
  • jQuery and Uploadify session in the php file
  • How to add System.Windows dll to Visual Studio 2010 express?
  • Cannot invoke my method on the array type int[]
  • Connecting to PostgreSQL in java app engine
  • Suqueries in select clause with JPA
  • Regarding starting the threads on a condition
  • 550 Access denied - Invalid HELO name
  • Query to find the duplicates between the name and number in table
  • SharedPreferences or SQLite Database?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • custom UITableViewCell with image for highlighting
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • How to access EntityManager inside Entity class in EJB3
  • req.body is undefined - nodejs
  • Spray.io: When (not) to use non-blocking route handling?
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • sending/ receiving email in Java
  • Akka Routing: Reply's send to router ends up as dead letters
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • AT Commands to Send SMS not working in Windows 8.1
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper way to use connect-multiparty with express.js?
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?