Summary of basic knowledge (3)

    10.18 
(1) The method of ajax sending data to the server
To send the request to the server, we use the open() and send() methods of the XMLHttpRequest object:
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
open( method , url , async ) specifies the type of request, the URL, and whether the request should be handled asynchronously. 
method : the type of request; GET or POST
url : the location of the file on the server
async : true (asynchronous) or false (synchronous)

send( stringsends the request to the server. 
string : only for POST requests

(2) The difference between post request and get
GET is simpler and faster than POST, and works in most cases.
However, use a POST request in the following cases:
Unable to use cache file (update file or database on server)
Send large amounts of data to the server (POST has no data limit)
POST is more stable and reliable than GET when sending user input containing unknown characters

10.19
(1) The difference between sleep and wait

1. The difference between sleep and wait in Java
① These two methods come from different classes , sleep comes from the Thread class, and wait comes from the Object class.
sleep is a static class method of Thread. Whoever calls it goes to sleep. Even if the sleep method of b is called in thread a, in fact, a goes to sleep. To make thread b sleep, sleep must be called in the code of b.
② Lock:  The most important thing is that the sleep method does not release the lock , and the wait method releases the lock, so that other threads can use the synchronization control block or method.
sleep does not give up system resources; wait is to enter the thread waiting pool to wait, sell system resources, and other threads can occupy the CPU. Generally, wait does not impose a time limit, because if the running resources of the wait thread are not enough, it is useless to come out again. It has to wait for other threads to call notify/notifyAll to wake up all the threads in the waiting pool, and then enter the ready queue and wait for the OS to allocate system resources. sleep(milliseconds) can be automatically woken up by specifying a time. If the time is not enough, it can only be interrupted by calling interrupt().
The role of Thread.sleep(0) is to "trigger the operating system to re-run a CPU competition immediately".
③ Scope of use: wait, notify and notifyAll can only be used in synchronous control methods or synchronous control blocks, while sleep can be used anywhere.
   synchronized(x){ 
      x.notify() 
     //or wait() 
   }
2 yield introduction 

The role of yield() is to yield. It can make the current thread enter the "ready state" from the "running state", so that other waiting threads with the same priority can obtain the right to execute; however, it does not guarantee that after the current thread calls yield(), other threads have the same priority. The thread will be able to obtain the right to execute; it is also possible that the current thread enters the "running state" and continues to run!

10.20 Comparable Interfaces
There is only one key method to be overridden inside the Comparable interface.
that is
int compareTo(T o)  
This method returns an Int value,  
For example i = x.compareTo(y)
If i=0, it also means that the objects x and y are ranked equal (it does not mean that x.equals(y) = true, but this is strongly recommended on the jdk api )
If the returned value i>0 means, x > y, 
Conversely, if i<0 means x < y

eg:
// The return value is an int value, if int>0 means this>o
//sort by name first, then by ranking
public int compareTo(Object o) {
             // TODO Auto-generated method stub
            Student s = (Student)(o); 
             int t= this . name .compareTo( s. name );
             if (t!=0)
            return t ;
             else
                   return ( this . ranking -( s. ranking )) ;
      }
10.21 Java线程状态 

java线程存在以下几种状态:

1: 创建状态(New):线程被new出来,还未调用start
2: 就绪状态(Runnable):又称为可执行状态,调用线程的start方法后,线程处于就绪状态,,线程调度程序还未给该线程分配cpu时间片执行。
3: 运行状态(Running):线程调度程序分配cpu时间片来执行线程代码。
4: 阻塞状态(Blocked):线程在运行过程中由于某种原因暂停运行进入阻塞状态,只有满足条件后进入就绪状态,获取cpu后才能再次进入运行状态。
阻塞的情况分三种:
A:等待阻塞(wait):调用wait()方法,与synchroined一起使用,线程进入对象等待池,释放synchroined的锁,处于阻塞状态。当有其他线程notify,notifyAll后线
                              进入锁标识等待池,即进入同步阻塞状态。
B:同步阻塞:线程运行过程中需要获取锁,但该锁被其他线程持有,则该线程进入锁标识等待池,处于同步阻塞状态。当线程获取锁之后,线程进入就绪状态。
C:其他阻塞:当线程sleep,或者join,或者发出I/O请求后,知道yield时间到,sleep时间到,join的线程执行完,或者I/O返回后,线程进入就绪状态。
5: 死亡状态(Dead):当线程Run方法退出或者运行出现异常线程停止时,线程就会消亡。

synchronized有两种用法,
一种是写在方法前,如果该方法是静态方法,则获取的锁是类锁,多线程调用该类中所有的实例的该方法都是互斥的。如果该方法不是静态的,则获取的锁是对象锁,多线程调用同一个实例的该方法是互斥的,调用不同实例则不是互斥的。
一种是同步代码块:synchronized(){//....},同样根据方法是否是静态方法区分获取对象锁还是类锁。

wait:与synchronized一起使用,即必须想获取指定的锁lock,才能lock.wait。wait时,会释放lock,线程进入对象等待池,释放synchroined的锁,处于阻塞状态。当有其他线程notify,notifyAll后线 进入锁标识等待池,即进入同步阻塞状态。

notify:与synchronized一起使用,即必须想获取指定的锁lock,才能lock.notify,synchronized代码块结束后释放锁,通知一个因lock而进入等待阻塞的线程进入同步阻塞状态。
notifyAll:与synchronized一起使用,即必须想获取指定的锁lock,才能lock.notifyAll,synchronized代码块结束后释放锁,会通知所有因Lock而进入等待阻塞的线程进入同步阻塞状态。
sleep:线程暂停运行,进入阻塞状态,但不会释放之前持有的锁,sleep能够让低优先级的线程有机会运行。
yield:线程进入就绪状态,不会释放锁,让同优先级的其他线程有机会运行。但下次可能继续分配cpu时间片,进入运行状态。
join:让一个线程B加入到一个线程A的尾部,在A运行完前,B不能运行。

10.26 
Java通过Executors提供四种线程池,分别为:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324750487&siteId=291194637