[Java] Multi-thread processing

Conclusion

What is a thread?

What is a process?

Process 1    | Thread 1..n

Memory configuration

1111_1.png

Thread Features-Independent / Memory Space Sharing

public class Main {
    String hoge = "hoge"; //This variable is shared
    public static void main(String[] args) {
        new Main().run();
    }
    private void run(){
        while (true) {
            System.out.println("1"); //Main thread
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("2");
        }
    }
}

Thread creation / execution

Inherit Thread class

public class MyThread extends Thread {
//Thread actual processing is a Thread derived class
  @Override
  public void run() {
    for (var i = 0; i < 30; i++) {
      //Get thread name with getName
      System.out.println(this.getName() + ": " + i);
    }
  }
}
public class ThreadBasic {

  public static void main(String[] args) {
    //Instantiate and create threads
    var th1 = new MyThread();
    var th2 = new MyThread();
    var th3 = new MyThread();
    //Start thread with start method
    th1.start();
    th2.start();
    th3.start();
  }
}

Runnable interface implementation

public class MyRunner implements Runnable {
  //Thread actual processing
  @Override
  public void run() {
    for (var i = 0; i < 30; i++) {
      System.out.println(Thread.currentThread().getName() + ": " + i);
    }
  }
}
public class RunnableBasic {

  public static void main(String[] args) {
    //Thread creation
    var th1 = new Thread(new MyRunner());
    var th2 = new Thread(new MyRunner());
    var th3 = new Thread(new MyRunner());
    //Thread start
    th1.start();
    th2.start();
    th3.start();
  }
}

Recommended Posts

[Java] Multi-thread processing
Java thread processing
Java string processing
[Java] Stream processing
java iterative processing
JAVA constructor call processing
Java random, various processing
Java
[Java] Timer processing implementation method
Measured parallel processing in Java
Understanding Java Concurrent Processing (Introduction)
Java
Summary of java error processing
Delegate some Java processing to JavaScript
Run node.js from android java (processing)
[Processing × Java] How to use variables
Server processing with Java (Introduction part.1)
Surprisingly deep Java list inversion-Stream processing
Basic processing flow of java Stream
Notes on Android (java) thread processing
Deleting files using recursive processing [Java]
[Processing × Java] How to use arrays
[Java] Processing time measurement method memo
[Java] Exception types and basic processing
Java learning (0)
Studying Java ―― 3
[Java] array
[Java] Annotation
[Java] Module
Java array
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
Data processing using stream API from Java 8
java (override)
java (method)
Java Day 2018
[Processing x Java] Construction of development environment
java (array)
Java static
Java serialization
JAVA paid
Java (set)
java shellsort
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
Date processing
Java features