Java basic interview: Set, internal class, thread

Source: Internet
Author: User

Java basic interview: Set, internal class, thread

package test;import java.util.Hashtable;import java.util.Map;public class test {public static String change(String param){param=null;return param;}public static void main(String[] args) {String param1="p1";param1=change(param1);Map table1=new Hashtable();table1.put(param1, "pv1");System.out.println(table1.get("p1"));}}/** * Exception in thread "main" java.lang.NullPointerExceptionat java.util.Hashtable.put(Hashtable.java:399)at test.test.main(test.java:15)*/


2.

package test;import java.util.ArrayList;import java.util.List;public class Test2 {public static void main(String[] args) {List list = new ArrayList();list.add("1");list.add("3");list.add("4");for(Object o : list){if ("3".equals(o)) {list.remove(o);}}for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}}}

Running output:

1

4



3. The interface can be new

Package test; public class Outer {public void instanceMethod () {Action action = new Action () {@ Overridepublic void doAction () {// TODO Auto-generated method stubSystem. out. println ("error") ;}}; action. doAction (); new DataClass (5) {public void printData () {// This method is not called System. out. println ("data =" + getData () ;};} public static void main (String [] args) {// TODO Auto-generated method stubnew Outer (). instanceMethod () ;}} interface Action {void doAction ();} class DataClass {private int data; public DataClass (int data) {this. data = data ;}public int getData () {return data ;}}

Running output:

Error


4. How many java multithreading methods can be implemented?

There are three types:
(1) inherit the Thread class and override the run Function
Create:
Class xx extends Thread {
Public void run (){
Thread. sleep (1000) // The Thread slews for 1000 milliseconds. sleep enables the Thread to enter the Block state and releases resources.
}}
Enable thread:
Object. start () // start thread, run Function
(2) Implement the Runnable interface and rewrite the run function.
Enable thread:
Thread t = new Thread (object) // create a Thread object
T. start ()
(3) Implement the Callable interface and rewrite the call function.
Callable is similar to Runnable interfaces. The classes that implement Callable interfaces and Runnable classes are all tasks that can be executed by other threads.
The differences between Callable and Runnable are as follows:
① Callable specifies the call () method, while Runnable specifies the run () method ().
② Callable tasks can return values after execution, while Runnable tasks cannot return values.
③ The call () method can throw an exception, while the run () method cannot throw an exception.
④ Run the Callable task to get a Future object. Future indicates the result of asynchronous calculation. It provides methods to check whether the computing is complete, and so on.
After the calculation is completed, the calculation result is retrieved. The Future object can be used to understand the task execution status, cancel the task execution, and obtain the task execution result.


5. What does Java thread synchronization mean?

One is to add sychronized before the method.
Public void sychronized start (){
System. out. println ("start ");
}

Add sychronized before the code snippet.
(Sychronized ){
.....
}

The synchronization method (the method modified by the synchronized keyword) can effectively solve the concurrency problem and avoid resource preemption, competition conditions, and deadlocks to a certain extent, however, the side effect is that synchronization locks can cause thread blocking. This requires that the execution time of the synchronization method cannot be too long.

This is the so-called lock mechanism. How do you use sychronized (Object obj) to lock an Object and release the lock after using the Object, other threads that need this object can be executed.

Zookeeper

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.