Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java Concurrency - multiple threads of HelloWorld     - Quickly build and install Linux KVM system (Linux)

- Linux system server network security management tips (Linux)

- Features and prevention methods elaborate network security grayware (Linux)

- How to build a custom exclusive Ubuntu Live CD (Linux)

- Use Docker containers (Linux)

- Memory leak analysis using Android studio (Programming)

- Installation CD audio file extraction tool Flacon (Linux)

- Ubuntu U disk do not have write privileges can only read but not write (Linux)

- VMware Workstation virtual machine cloning (Linux)

- Analyzing Linux server architecture is 32-bit / 64-bit (Server)

- CentOS 7 open ports (Linux)

- Local port forwarding using iptables under linux (Server)

- Oracle 11g upgrade PSU detailed steps (Database)

- How to use the ps command to monitor progress in the implementation of Linux commands (Linux)

- 127.0.0.1 and localhost difference (Server)

- Ubuntu derivative version of the user and how to install SmartGit / HG 6.0.0 (Linux)

- Digital jQuery scrolling effect (Programming)

- CentOS 6 Install Xen4.2 Virtualization Practice (Linux)

- Linux Getting Started tutorial: Experience QEMU virtual machine articles (Linux)

- Java Annotation Comments (Programming)

 
         
  Java Concurrency - multiple threads of HelloWorld
     
  Add Date : 2018-11-21      
         
       
         
  In two ways: First, to achieve Runnable interface; Second, the basis Thread class.

First, to achieve Runnable interface

package com.tgb.klx.thread;

public class hello1 implements Runnable {

 public hello1 () {

 }

 public hello1 (String name) {
  this.name = name;
 }

 public void run () {
  for (int i = 0; i < 5; i ++) {
   System.out.println (name + "Run" + i);
  }
 }

 public static void main (String [] args) {
  hello1 h1 = new hello1 ( "Thread A");
  Thread demo1 = new Thread (h1);
  hello1 h2 = new hello1 ( "thread B");
  Thread demo2 = new Thread (h2);
  demo1.start ();
  demo2.start ();
 }

 private String name;

}

Second, based on the Thread class

package com.tgb.klx.thread;
public class hello2 extends Thread {

 public hello2 () {

 }

 public hello2 (String name) {
  this.name = name;
 }

 public void run () {
  for (int i = 0; i < 5; i ++) {
   System.out.println (name + "Run" + i);
  }
 }

 public static void main (String [] args) {
  hello2 h1 = new hello2 ( "A");
  hello2 h2 = new hello2 ( "B");
  h1.start ();
  h2.start ();
  
 }

 private String name;
}

Examples of ways to achieve Runnable interface, you need to create a Thread class will realize runnable class as a parameter in, start a thread, if called directly runnable's run method calls with no distinction between ordinary class methods, does not create a new thread .

Thread class implements Runnable interface, Thread class also has run method call Thread's run method also does not create a new thread and call an ordinary method no difference, so when we must pay attention to the use of multiple threads.

to sum up:

These two methods can achieve, the specific choice of which way the decision according to the situation. Inside java does not support multiple inheritance, so to achieve runnable interface may be a little more flexible way.
     
         
       
         
  More:      
 
- Android Scroller call mechanism and the relationship of computeScroll (Programming)
- Linux RPM default installation path (Linux)
- Java annotations entry automatically generates SQL statements (Programming)
- ORA-38856: Unable instance UNNAMED_INSTANCE_2 (redo thread 2) marked enabled (Database)
- JavaScript in this usage (Programming)
- Servlet 3.0 interfaces of AsyncListener (Programming)
- MySQL script incremental backups (innobackupex) (Database)
- Ubuntu way of decompressing files (Linux)
- Redis application of Sina Weibo (Database)
- Programmers Do not neglect debugging techniques (Programming)
- Help you to see Linux system architecture type 5 Common Commands (Linux)
- Linux use glibc2 library and crypt () function to generate the user password (Programming)
- LinkedList Basic Usage (Programming)
- JDK installation and configuration environment variable under linuxb (Linux)
- Java memory area Explanation (Programming)
- How to experience Unity 8 Mir on Ubuntu 16.04 (Linux)
- Distributed Firewall Design on Linux platform (Linux)
- How to understand the difference between synchronous and asynchronous non-blocking blocking (Programming)
- Linux Getting Started Tutorial: How to set up a static MAC address on VMware ESXi virtual machine (Mobile)
- C ++ multithreading and critical resource instance (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.