2017-9-1-a-Exception Handling-Multithreading basics

Source: Internet
Author: User
Tags stub

1 can I throw an exception on my own? When do I need it?
Yes, for example, after handling an exception, throw an exception and let the last layer of exception handling block snap.

2 How do I set an exception? In the method body set the thrown exception, must also be thrown in the declaration? How do I add? When the throws method is added, does the test code have to handle the exception when calling the method?
public void Setage (int.) {
/*
* When incoming parameters do not meet the business logic requirements
* For example, if the number of the age is not 0-100, it does not meet the requirements,
* Can be thrown to the caller as an exception
*
* When the posted params is not setisfy the
*/

if (Age < 0| | Age > 100) {
throw new RuntimeException ("Age is not legal");
}

This.age = age;
}

must, except runtimeexception can not add.

public void Setage (int.) throws exception{
if (Age < 0| | Age > 100) {
throw new Exception ("Age is not legal");
}
This.age = age;
}

public static void Main (string[] args) {
Person p = new person ();
try {
P.setage (100);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println (P.getage ());

When do I need try...catch ... Catch handling exception?
When you need to handle the exception.

3 How do I understand the relationship between throwing exceptions and handling exceptions?
There may be errors in the method's internal code that define the possible exceptions, the method internally defines the exception that needs to be declared at the method definition, and the method that declares the exception must handle the exception, there are two ways to handle the exception, and when the exception is handled by the calling method's code block, use Try ... Catch captures and processes
The exception can continue to be thrown out when it is not processed at the call.

4 Can the code be executed?
Person p = new person ();
try {
Exception e;
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println (P.getage ());
Yes, catch the exception and handle it, which is thrown to handle.

4 Subclass What is the rule when overriding a parent class that contains a throw declaration method?
Do not throw any exception, can throw a part (throws can throw a variety of exceptions, separated by commas), can throw subclass exception,
But cannot throw extra exceptions (the parent class does not have an exception), the parent exception of the parent class
That is, the exception that can throw an exception must be less than or equal to the parent class

5 exceptions can be divided into?
Check for exceptions and do not check for exceptions (RuntimeException and its subclasses)

What are the 7 exception APIs?
Output error flow on console--to track errors:
Printstacktrace ()
———— using the example
try {
String str = "ABC";
SYSTEM.OUT.PRINTLN (Integer.parseint (str));
} catch (Exception e) {
E.printstacktrace ();
}

The direct cause of the output error (in fact, the internal string returned when the exception was set) throw new RuntimeException ("Age is not legal");--as the above code is because the ABC is entered,
E.getmessage ()
———— Return Example
"For input string:" abc ""

8 How do I customize exceptions?
A, naming exceptions
b, inherit the exception or custom exception.
C, generates a parent class construct.

public class Illegalageexception extends exception{

public static void Main (string[] args) {

}

Public Illegalageexception () {
Super ();
TODO auto-generated Constructor stub
}

Public illegalageexception (String message, Throwable cause, Boolean enablesuppression, Boolean writablestacktrace) {
Super (message, cause, enablesuppression, writablestacktrace);
TODO auto-generated Constructor stub
}
...
}

}

What is the working mechanism of 9 multi-threading?
Fast switching between multiple tasks with high performance.

10 What is a process?
The task that the operating system is running. The process actually contains some voluntary memory resources.

11 Threads Yes?
is a service that runs concurrently within a process. Therefore, the call is within the same process of the storage.

12 How do I create a new thread in Java? How do I start a thread? How do I understand threads?
Extends Thread

public class ThreadDemo1 {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Thread T1 = new MyThread1 ();
Thread t2 = new MyThread2 ();
/*
* The start thread calls the Start method. Do not call the thread's Run method directly.
* When the Start method is finished, the thread is scheduled for thread, once the thread gets the CPU time slice
* (Execution time) to start operation is to automatically call your own Run method to start working.
*/
T1.start ();
T2.start ();
}

}

Threads are used to solve problems that run multiple programs at the same time

13 How do I resolve the conflict between thread and single inheritance?
Multithreading is implemented using the implementation runnable interface.

public class ThreadDemo2 {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Runnable r1 = new myrunnable ();
Runnable r2 = new MyRunnable2 ();

thread T1 = new Thread (r1);
Thread t2 = new Thread (r2);

T1.start ();
T2.start ();
}

}

Class Myrunnable implements runnable{
public void Run () {
for (int i = 0;i < 100;i++) {
System.out.println ("Look at what");
}
}
}

Class MyRunnable2 implements runnable{
public void Run () {
for (int i = 0;i < 100;i++) {
System.out.println ("Look at you!");
}
}
}


14 Create an anonymous inner class for the thread instance in two ways, respectively.
public class ThreadDemo3 {

public static void Main (string[] args) {
thread T1 = new Thread () {
public void Run () {
for (int i = 0;i < 100;i++) {
System.out.println ("Look at what");
}
}
};

Runnable r2 = new Runnable () {
public void Run () {
for (int i =0;i < 100;i++) {
System.out.println ("Look at you!");
}
}
};
Thread t2 = new Thread (r2);

T1.start ();
T2.start ();

}

}

15 How do I get the thread that is currently running a method call?
Thread t = Thread.CurrentThread ();

All methods in Java are run on a thread, and the child methods within the method call the same thread, unless you create a new thread within the method and call other methods within the new thread.
At this point the thread of the other method is the newly created thread

16 How do I interrupt the current process?
T.interrupt ();

17 How do I get information about the current thread?
Thread name, thread ID, priority, thread activity status, whether it is a daemon thread, whether it is interrupted
Thread T = Thread.CurrentThread ();
String name = T.getname ();
Long id = T.getid ();
int priority = T.getpriority ();
Boolean isAlive = T.isalive ();
Boolean Isdaemon = T.isdaemon ();
Boolean isinterrupt = t.isinterrupted ();

18 How do I set thread priority?
/**
* Thread Priority
*
* Thread Priority has 10 levels, with integer 1-10 for
* 1 lowest, 10 highest, 5-bit default priority level
*
* Because threads cannot interfere with thread scheduling, i.e.: When the CPU cannot be actively acquired Can not determine the length of time slices.
* So you can only maximize the chance of getting a CPU time slice by adjusting the priority
* Theoretically, the higher the thread priority, the more times the time slice is obtained.
* @author Admin
*
*/

19 What is a daemon thread? How do I define a daemon thread?
That is, the background thread
public static void Main (string[] args) {
Thread rose = new Thread () {
public void Run () {
for (int i=0;i<5;i++) {
System.out.println ("Rose:let Me go!");
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
System.out.println ("Rose: Ah ah ah ah aaaaaaaaaaaa ...");
System.out.println ("Effect: poof");
}
};

Thread jack = new Thread () {
public void Run () {
while (true) {
System.out.println ("Jack:you jump! I jump! ");
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
};
Set the background thread to set before start
Jack.setdaemon (TRUE);

Rose.start ();
Jack.start ();

}

The default value after the Long,float data definition is?
0L (l not shown)
0.0f (f not shown)

int a = 10;a = a++ + 10;A++;SYSO (a)?
The result is 21, and the second step, though self-increasing, is finally re-assigned to a.

22 How do I output the time in "time: minutes: Seconds" format?
Gets the number of milliseconds in the current time, passed into the format conversion function in the formatted SDF, and the output is a string of that form.
Date date = new Date ();
Long time = Date.gettime ();
SimpleDateFormat trans = new SimpleDateFormat ("HH:mm:ss");
System.out.println (Trans.format (time));

23 How to delay time?
Use Thread.Sleep ().
————
21st

To-dos list
① review of the second stage all content
② Complete Daily Exercises
③ Review the first stage content

2017-9-1-a-Exception Handling-Multithreading basics

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.