Java-some simple methods and usage of exception handling and threading

Source: Internet
Author: User

1.1 Subclasses override the rule when the parent class contains a method that throws declares an exception throw:
1. Allow no exceptions to be thrown.
2. Only partial exceptions are thrown.
3. Throws a parent class method that throws an exception for the subtype exception.
4. No additional exceptions can be thrown.
5. The parent type exception cannot be thrown by a method that throws an exception.
Usually when a method throws an exception using throw, it is necessary to use throws on the current method to define the throw of the exception to inform the caller. Only RuntimeException and its subclasses can be undefined when thrown, otherwise they must be defined.
1.2 When invoking a method that contains a throws declaration exception, the compiler requires that the exception be handled by the means that it is not compiled.
* There are two ways of handling:
* 1: Use Try-catch to capture and handle the exception
* 2: Continue using throws to throw the exception on the current method.
Some methods of exceptions: E.printstacktrace (); Output error stack information to help locate the code in error to adjust the program error.
String E.getmessage () Gets the error message, usually the cause of the error.
1.3 Custom exceptions, typically errors that are used to define the level of business logic in your project. It is necessary to rewrite the entire construction method of the exception to ensure ease of use.
Simple understanding of 2.1 threads:
Multithreading: Multithreading allows multiple pieces of code to run simultaneously (concurrently).
There are two ways of creating a thread
Way one: Inherit the thread and rewrite the Run method.
Example: Class MyThread1 extends thread{
public void Run () {
Business logic
}
}
Note: This method of creating threads has two disadvantages:
1: Because Java is a single inheritance, this results in inheriting the thread and can no longer inherit other classes. The actual development will cause a lot of inconvenience.
2: Because the Run method is rewritten, the task code executed by the thread is defined in the Run method, which causes the thread to have a certain coupling relationship with the task. Not conducive to thread reuse.
Mode two: Implement the Runnable interface to define tasks separately.
Example: Class MyRunnable1 implements runnable{
public void Run () {
Business logic
}
}
This way of creating threads compensates for the disadvantage of not inheriting other classes, separating the threads from the business logic.
Both of these methods can be created using the creation of anonymous inner classes.
The start thread calls the Start method. Do not call the thread's Run method directly.
When the Start method finishes executing, the thread is scheduled for thread, and once the thread gets the CPU time slice begins to run, it automatically calls its own Run method to start working.

Static methods provided by the thread:
Static thread CurrentThread () This method can get the thread that is running the method.
static void sleep (long ms) This method can block a thread that is running the current method for a specified millisecond
Some information can be obtained through the thread:
Thread T = thread.currentthread ();//Gets the thread that runs the main method
String name = T.getname ();//Get Thread name
Long id = T.getid ();//Get thread ID (unique indication)
int priority = T.getpriority ();//Thread precedence
Boolean isAlive = T.isalive ();//Thread activity status
Boolean Isdaemon = T.isdaemon ();//is the daemon thread
Boolean isinterrupted = t.isinterrupted ();//whether interrupted
Thread Priority: There are 10 levels of thread priority, denoted by an integer 1-10, respectively. Where 1 is the lowest, 10 is highest, and 5 is the default priority.
Because the thread cannot interfere with the thread scheduling work, namely: cannot take the CPU time actively, also cannot decide the time slice length. So you can only adjust the priority to maximize the chance of getting CPU time slices. The higher the thread priority thread, the more times it takes to get the CPU time slices.
Daemon Thread: When a process finishes, all the running daemons force the end of the process when all foreground threads in a process end. As a result, tasks that can be run all the time, but those that can be ended together when the program's primary work is finished, are run on the daemon thread.
Jack.setdaemon (TRUE); Set the background thread to set before start

I am a beginner, if the update is not good, welcome the Great God pointed out, thank you!

More exciting after the update, reprinted annotated!

Java-some simple methods and usage of exception handling and threading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.