Java (exception handling, threading, collection, file IO)

Java programming

1. Exception handling

An error will occur on Java if you perform unexpected processing such as dividing by 0. (Runtime error) It is necessary to use try, catch, finally blocks as a way to handle exception error handling on java.

try~catch statement


try{
try block
The process you want to execute
}
catch(Exception type Variable name) {
catch block
What to do when an exception is thrown
}
finally {
finally block
The last process that must be done
}

sample1(Division by zero)


public class test1 {

	public static void main(String[] args) {
		//TODO auto-generated method stub
		int a=2;
		int b=100;

		try {
			int c = b/a;
			System.out.println(c);
		}
		catch(ArithmeticException e) {
			System.out.println("Exception occurred");
			System.out.println(e);
			return;
		}
		finally {
			System.out.println("End Program");
		}
	}
}

sample2(Non-existent array specification)


class SimpleClass{
	void doSomething() {
		int array[] = new int[3];
		array[10]=999;
		System.out.println("Exit the doSomething method");
	}
}

public class test2 {
	public static void main(String[] args) {
		SimpleClass obj = new SimpleClass();
		try {
			obj.doSomething();
		}
		catch(ArrayIndexOutOfBoundsException e) {
			System.out.println("Exception occurred");
			System.out.println(e);
			return;
		}
		finally {
			System.out.println("End Program");
		}
	}
}

2. Multithreading

Previous programs had only one thread. This thread starts processing from the main method and executes statements in order from the top. This thread is called the main thread. This is called a single-threaded program, but it is also possible to create a program that executes two or more instructions at the same time. This is called a multithreaded program. An image in which there are two or more program processing flows, each of which progresses in parallel.

The following is a program that transfers 10,000 yen per person to a bank account and displays the total amount of money for 100 people. In order to execute a multithreaded program, it is necessary to inherit the Thread class and override the run method to define the specified process. If you create an instance of Thread class in the main method and call the start method, you can finally execute it.

In addition, exclusive control is performed by adding the syncronized qualifier to the join method to wait for the thread processing to finish, and to the addOneYen method so that only one thread can be executed at a time.

Multithreaded example


class Bank{

	static int money=0;
	static synchronized void addOneYen() {
		money++;
	};

}
class Customer extends Thread {
	public void run() {
		for(int i=0;i<10000;i++) {
			Bank.addOneYen();
		}
	}
}
public class test4 {
	public static void main(String[] arg) {
		Customer[] customer=new Customer[100];

		for ( int i=0;i<100;i++) {
			customer[i]=new Customer();
			customer[i].start();
		}
		for(int i=0;i<100;i++) {
			try {
				customer[i].join();
			}catch(InterruptedException e) {
				System.out.println(e);
			}
		}
		System.out.println(Bank.money);
	}


}


Recommended Posts

Java (exception handling, threading, collection, file IO)
Java exception handling?
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
[Java] Practice of exception handling [Exception]
[Java] About try-catch exception handling
Java exception handling usage rules
Exception handling techniques in Java
[In-house study session] Java exception handling (2017/04/26)
Step-by-step understanding of Java exception handling
[For Java beginners] About exception handling
☾ Java / Collection
Java9 collection
Exception handling
Exception handling Exception
try-catch-finally exception handling How to use java
About exception handling
ruby exception handling
Java Reintroduction-Java Collection
Ruby exception handling
java file creation
Java IO review
Questions in java exception handling throw and try-catch
[Java] Collection framework
[Java Silver] (Exception handling) About try-catch-finally and try-with-resource statements
[Java] File system operation
Expired collection of java
About Ruby exception handling
Java collection interview questions
Read Java Property file
[java] throw an exception
java (split source file)
java core: chopped core file
Chapter 2 Network programming with JAVA phttpd Exception collection in 3 places