bzdww

Get answers and suggestions for various questions from here

Java Intern Interview Questions (7)

cms

After 11 long holidays, all work should gradually resume the track, continue to do what you should do in your position, continue to share the java interns interview questions, I wish everyone is ready to find in the gold nine silver The working partners can find a job that they are satisfied with!

This interview question: Interview questions for multi-threaded knowledge.



1. There are three threads T1, T2, T3, how to ensure that T2 is executed after T1 is executed, and T3 is executed after T2 is executed?

A: Use the join method.

The function of the join method is to make the asynchronously executed thread into synchronous execution. That is, after calling the start method of the thread instance, the method will return immediately. If you need to use a value calculated by this thread after calling the start method, you must use the join method. If you do not use the join method, there is no guarantee that this thread will be executed when executing a statement after the start method. After using the join method, the program will not execute until the thread exits.

2. The Lock interface in Java, compared to synchronized, where is the advantage?

If you need to implement an efficient cache, it allows multiple users to read, but only allows one user to write, in order to maintain its integrity, how to achieve?

The biggest advantage of the Lock interface is that it provides locks for reading and writing, respectively.

The read-write lock ReadWriteLock has more powerful features, which can be subdivided into read locks and unlocks.

A read lock can allow multiple threads that perform read operations to enter at the same time, but does not allow the write process to enter; a write lock allows only one write process to enter, during which no process can enter. (Completely meet the conditions in the title that allow multiple users to read and one user to write)

It should be noted that each read-write lock has a padlock and unlock. It is best to use try and finally to insert the middle code into each pair of padlocks and unlocks. This will prevent the deadlock caused by the exception. Happening.

Here is a sample program:

```

Import java.util.Random;

Import java.util.concurrent.locks.*;

Public class ReadWriteLockTest {

Public static void main(String[] args) {

Final TheData myData=new TheData(); //This is the shared data for each thread

For(int i=0;i<3;i++){ //Open 3 read threads

New Thread(new Runnable(){

@Override

Public void run() {

While(true){

myData.get();

}

}

}).start();

}

For(int i=0;i<3;i++){ //Open 3 write threads

New Thread(new Runnable(){

@Override

Public void run() {

While(true){

myData.put(new Random().nextInt(10000));

}

}

}).start();

}

}

}

Class TheData{

Private Object data=null;

Private ReadWriteLock rwl=new ReentrantReadWriteLock();

Public void get(){

rwl.readLock().lock(); //Read lock is enabled, the read thread can enter

Try { // use try finally to prevent deadlock caused by exception

System.out.println(Thread.currentThread().getName()+"is ready to read");

Thread.sleep(new Random().nextInt(100));

System.out.println(Thread.currentThread().getName()+"have read date"+data);

} catch (InterruptedException e) {

e.printStackTrace();

} finally{

rwl.readLock().unlock(); //Read lock unlock

}

}

Public void put(Object data){

rwl.writeLock().lock(); //Write lock is enabled, then only one write thread enters

Try {

System.out.println(Thread.currentThread().getName()+"is ready to write");

Thread.sleep(new Random().nextInt(100));

This.data=data;

System.out.println(Thread.currentThread().getName()+"have write date"+data);

} catch (InterruptedException e) {

e.printStackTrace();

} finally{

rwl.writeLock().unlock(); //Write lock to unlock

}

}

}


```

3. What is the difference between the wait and sleep methods in java?

The big difference is that wait will release the lock while waiting, and sleep will hold the lock. Wait is usually used for inter-thread interactions, and sleep is usually used to suspend execution.

Other differences are:

- sleep is a static method of the Thread class, and wait is an Object method.

- wait, notify, and notifyAll can only be used in synchronous control methods or synchronous control blocks, while sleep can be used anywhere

- sleep must catch exceptions, while wait, notify, and notifyAll don't need to catch exceptions.

To be continued...