Java Foundation interview: Collections, inner classes, threads

Source: Internet
Author: User

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";p Aram1=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));}}}

Execution output:

1

4



3. Interface is capable of 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 () {//The method was 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;}}

Execution output:

Error


4. Arethere several ways to implement Java multithreading?

There are three kinds:
(1) Inheriting the thread class, overriding the Run function
Create:
Class XX extends thread{
public void Run () {
Thread.Sleep (1000)//thread sleeps for 1000 milliseconds, sleep causes the thread to enter the block state, and frees the resource
}}
To open a thread:
Object. Start ()//start thread. The Run function executes
(2) Implementing the Runnable interface, overriding the Run function
To open a thread:
Thread t = new Thread (object)//Create thread object
T.start ()
(3) Implement callable interface, rewrite call function
Callable are similar to runnable interfaces, classes that implement callable interfaces, and classes that implement runnable, are tasks that can be run by other threads.
There are several differences between callable and runnable:
The method prescribed by ①callable is call (), and the method specified by runnable is run ().
The ②callable task can return a value after it is run, and Runnable's task is not to return a value
The ③call () method throws an exception, and the run () method cannot throw an exception.
④ perform callable tasks to get a future object. The future represents the result of an asynchronous calculation. It provides a way to check whether the calculation is complete, etc.
The calculation is completed and the results of the calculation are retrieved. The future object can be used to understand the task, cancel the task, and get the result of the task running.


5.What does Java thread sync mean?

One is the method before adding sychronized
public void sychronized Start () {
System.out.println ("Start");
}

Another is to add sychronized before the code snippet
(sychronized) {

。。

。。


}

The synchronous method (Synchronizedkeyword modified method) can solve the concurrency problem well, and can avoid the situation of resource preemption, competition condition and deadlock to some extent. But the side effect is that a synchronous lock can cause a thread to clog. This requires that the synchronization method not run too long.

This is called the locking mechanism. Why you use Sychronized (object obj) to lock an object. Once you have finished using this object, the lock is released. Other threads that need the object are capable enough to run.

??

Java Foundation interview: Collections, inner classes, threads

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.