A memo for my study.
Area for storing variable values, generated instances, history of called methods, etc. Where to store these is managed by the JVM. There are two types of memory area
--Stack --Heap
There is.
Method call history, value of variable (local variable) declared in the method. When the method finishes processing, that information is removed from the stack.
An instance generated from a class. When the instance is no longer referenced by any variable (change reference, assign null) In addition, it is automatically released by the garbage collection of the JVM.
There are the following two types.
--Single thread
There are two methods
--Create a new class that inherits the Thread class --Create a new class that implements the Runnable interface.
Create an instance of a class that inherits the Thread class Call the start method of that instance.
It's the start method that executes, In the subclass that inherits the Thread class, Override the run method of Thread class.
The run method is executed via the start method. Results are not always the same and change each time they are run (sometimes they don't)
class MyTread extends Thread{
//NOTE: The level cannot be lowered below the access modifier defined in the superclass.
//You can't reduce the visibility of methods inherited from Thread
public void run() {
for(int i = 0; i < 5; i++) {
System.out.println("MyTread.run:"+i);
}
}
}
public class SampleProgram{
public static void main(String[] args) {
MyTread mt = new MyTread();
mt.start();
for(int i = 0; i < 5; i++) {
System.out.println("main:"+i);
}
}
}
//result
main:0
main:1
main:2
MyTread.run:0
MyTread.run:1
MyTread.run:2
MyTread.run:3
main:3
MyTread.run:4
main:4
It's not much different from when it was inherited and made.
class MyTread implements Runnable{
public void run() {
for(int i = 0; i < 5; i++) {
System.out.println("MyTread.run:"+i);
}
}
}
public class SampleProgram{
public static void main(String[] args) {
MyTread mt = new MyTread();
Thread t = new Thread(mt);
t.start();
for(int i = 0; i < 5; i++) {
System.out.println("main:"+i);
}
}
}
Stops thread processing for a certain period of time. The unit is milliseconds. 1000: 1 second 2000: 2 seconds
In microseconds 1000000: 1 second
Since the Thread.sleep method can throw an Exception of type InterruptedException I have to catch it.
public class SampleProgram{
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("main:"+i);
}
}
}
//I tried it with the one that implemented the Runnable interface
class MyTread implements Runnable {
public void run() {
for(int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//TODO auto-generated catch block
e.printStackTrace();
}
System.out.println("MyTread.run:"+i);
}
}
}
public class SampleProgram{
public static void main(String[] args) {
MyTread mt = new MyTread();
Thread t = new Thread(mt);
t.start();
for(int i = 0; i < 5; i++) {
System.out.println("main:"+i);
}
}
}
join()
You can wait for the thread to process.
Since the join method can throw an Exception of type InterruptedException I have to catch it.
After all the processing of the MyTread class is completed, the main processing is executed.
class MyTread implements Runnable {
public void run() {
for(int i = 0; i < 5; i++) {
System.out.println("MyTread.run:"+i);
}
}
}
public class SampleProgram{
//Now let's throws.
public static void main(String[] args) throws InterruptedException {
MyTread mt = new MyTread();
Thread t = new Thread(mt);
t.start();
t.join();
for(int i = 0; i < 5; i++) {
System.out.println("main:"+i);
}
}
}
When the processing of the run method is terminated, the processing of that thread is terminated. Hmm ... let's leave it for now.
When running in multiple threads, if multiple threads share the same variable value Inconsistencies may occur.
The following is a program whose total amount will be 1000000 yen
class Bank{
static int money = 0;
static void addOneYen() {
money++;
}
}
class Customer extends Thread{
public void run() {
for(int i = 0; i < 10000; i++) {
Bank.addOneYen();
}
}
}
public class MultiThreadEx {
public static void main(String[] args) {
Customer[] customers = new Customer[100];
for(int i = 0; i < 100; i++) {
customers[i] = new Customer();
customers[i].start();
}
for(int i = 0; i < 100; i++) {
try {
customers[i].join();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Bank.money["+Bank.money+"]");
}
}
//result
Bank.money[628887]
Because there is no consistency between each threat at the timing of referencing / updating the current amount. The total amount goes wrong. In such a case, add "synchronized" before the addOneYen method.
static synchronized void addOneYen() {
money++;
}
If you prefix the method with synchronized, you can only execute one thread at a time. When one threat is processing, the other threat waits until its turn.
Recommended Posts