java并发之如何解决线程安全问题

×
id="BAIDU_DUP_fp_iframe" src="https://pos.baidu.com/wh/o.htm?ltr=">
首页 > 程序开发 > 软件开发 > Java > 正文
java并发编程:线程安全-线程同步-synchronized和lock
2016-09-16 09:32:29      0 个评论    来源:公绪凯—个人专栏  
收藏   我要投稿

多线程在提高效率的同时,必然面临线程安全的问题,Java中提供了一些机制来解决线程安全问题。

当多个线程同时访问临界资源(或叫共享资源)(一个对象,对象中的属性,一个文件,一个数据库等)时,就可能会产生线程安全问题。

不过,当多个线程执行一个方法,方法内部的局部变量并不是临界资源,因为方法是在栈上执行的,而Java栈是线程私有的,因此不会产生线程安全问题。

解决方案:序列化访问临界资源”的方案,即在同一时刻,只能有一个线程访问临界资源,也称作同步互斥访问。

在Java中,提供了两种方式来实现同步互斥访问:synchronized和Lock。

1.synchronized

(1)synchronized方法

例子:两个线程分别调用insertData对象插入数据:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Test {
  
     public static void main(String[] args)  {
         final InsertData insertData = new InsertData();
          
         new Thread() {
             public void run() {
                 insertData.insert(Thread.currentThread());
             };
         }.start();
          
          
         new Thread() {
             public void run() {
                 insertData.insert(Thread.currentThread());
             };
         }.start();
    
}
  
class InsertData {
     private ArrayList<integer> arrayList = new ArrayList<integer>();
      
     public void insert(Thread thread){
         for ( int i= 0 ;i< 5 ;i++){
             System.out.println(thread.getName()+ "在插入数据" +i);
             arrayList.add(i);
         }
     }
}</integer></integer>
此时程序的输出结果为:
\

 

说明两个线程在同时执行insert方法。

而如果在insert方法前面加上关键字synchronized的话,运行结果为:

 

?
1
2
3
4
5
6
7
8
9
10
class InsertData {
     private ArrayList<integer> arrayList = new ArrayList<integer>();
      
     public synchronized void insert(Thread thread){
         for ( int i= 0 ;i< 5 ;i++){
             System.out.println(thread.getName()+ "在插入数据" +i);
             arrayList.add(i);
         }
     }
}</integer></integer>

\

 

 

从上输出结果说明,Thread-1插入数据是等Thread-0插入完数据之后才进行的。说明Thread-0和Thread-1是顺序执行insert方法的。

这就是synchronized方法。

注意:

1)当一个线程正在访问一个对象的synchronized方法,那么其他线程不能访问该对象的其他synchronized方法。这个原因很简单,因为一个对象只有一把锁,当一个线程获取了该对象的锁之后,其他线程无法获取该对象的锁,所以无法访问该对象的其他synchronized方法。

  2)当一个线程正在访问一个对象的synchronized方法,那么其他线程能访问该对象的非synchronized方法。这个原因很简单,访问非synchronized方法不需要获得该对象的锁,假如一个方法没用synchronized关键字修饰,说明它不会使用到临界资源,那么其他线程是可以访问这个方法的,

  3)如果一个线程A需要访问对象object1的synchronized方法fun1,另外一个线程B需要访问对象object2的synchronized方法fun1,即使object1和object2是同一类型),也不会产生线程安全问题,因为他们访问的是不同的对象,所以不存在互斥问题。

(2)synchronized代码块

synchronized代码块类似于以下这种形式:

synchronized(synObject) { }

当在某个线程中执行这段代码块,该线程会获取对象synObject的锁,从而使得其他线程无法同时访问该代码块。

  synObject可以是this,代表获取当前对象的锁,也可以是类中的一个属性,代表获取该属性的锁。

  比如上面的insert方法可以改成以下两种形式:

?
1
2
3
4
5
6
7
8
9
10
11
12
<span style= "font-size:14px;" > class InsertData {
     private ArrayList<integer> arrayList = new ArrayList<integer>();
      
     public void insert(Thread thread){
         synchronized ( this ) {
             for ( int i= 0 ;i< 100 ;i++){
                 System.out.println(thread.getName()+ "在插入数据" +i);
                 arrayList.add(i);
             }
         }
     }
}</integer></integer></span>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
<span style= "font-size:14px;" > class InsertData {
     private ArrayList<integer> arrayList = new ArrayList<integer>();
     private Object object = new Object();
      
     public void insert(Thread thread){
         synchronized (object) {
             for ( int i= 0 ;i< 100 ;i++){
                 System.out.println(thread.getName()+ "在插入数据" +i);
                 arrayList.add(i);
             }
         }
     }
}</integer></integer></span>
从上面可以看出,synchronized代码块使用起来比synchronized方法要灵活得多。synchronized代码块可以实现只对需要同步的地方进行同步。

 

另外,每个类也会有一个锁,它可以用来控制对static数据成员的并发访问。

  并且如果一个线程执行一个对象的非static synchronized方法,另外一个线程需要执行这个对象所属类的static synchronized方法,此时不会发生互斥现象,因为访问static synchronized方法占用的是类锁,而访问非static synchronized方法占用的是对象锁,所以不存在互斥现象。

看下面这段代码就明白了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Test {
  
     public static void main(String[] args)  {
         final InsertData insertData = new InsertData();
         new Thread(){
             @Override
             public void run() {
                 insertData.insert();
             }
         }.start();
         new Thread(){
             @Override
             public void run() {
                 insertData.insert1();
             }
         }.start();
    
}
  
class InsertData {
     public synchronized void insert(){
         System.out.println( "执行insert" );
         try {
             Thread.sleep( 5000 );
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         System.out.println( "执行insert完毕" );
     }
      
     public synchronized static void insert1() {
         System.out.println( "执行insert1" );
         System.out.println( "执行insert1完毕" );
     }
}
执行结果:

\

第一个线程里面执行的是insert方法,不会导致第二个线程执行insert1方法发生阻塞现象。

注意:对于synchronized方法或者synchronized代码块,当出现异常时,JVM会自动释放当前线程占用的锁,因此不会出现由于异常导致出现死锁现象。
 

2.Lock

synchronized是java中的一个关键字,也就是说是Java语言内置的特性。

 

代码块被synchronized修饰了,当一个线程获取了对应的锁,并执行该代码块时,其他线程便只能一直等待,等待获取锁的线程释放锁,而这里获取锁的线程释放锁只会有两种情况:

  1)获取锁的线程执行完了该代码块,然后线程释放对锁的占有;

  2)线程执行发生异常,此时JVM会让线程自动释放锁。

如果这个获取锁的线程由于要等待IO或者其他原因(比如调用sleep方法)被阻塞了,但是又没有释放锁,其他线程便只能等待,多么影响程序执行效率。

因此就需要有一种机制可以不让等待的线程一直无期限地等待下去(比如只等待一定的时间或者能够响应中断),通过Lock就可以办到。

 

再举个例子:当有多个线程读写文件时,读操作和写操作会发生冲突现象,写操作和写操作会发生冲突现象,但是读操作和读操作不会发生冲突现象。

  但是采用synchronized关键字来实现同步的话,就会导致一个问题:

  如果多个线程都只是进行读操作,所以当一个线程在进行读操作时,其他线程只能等待无法进行读操作。

  因此就需要一种机制来使得多个线程都只是进行读操作时,线程之间不会发生冲突,通过Lock就可以办到。

  另外,通过Lock可以知道线程有没有成功获取到锁。这个是synchronized无法办到的。

  总结一下,也就是说Lock提供了比synchronized更多的功能。但是要注意以下几点:

  1)Lock不是Java语言内置的,synchronized是Java语言的关键字,因此是内置特性。Lock是一个类,通过这个类可以实现同步访问;

  2)Lock和synchronized有一点非常大的不同,采用synchronized不需要用户去手动释放锁,当synchronized方法或者synchronized代码块执行完之后,系统会自动让线程释放对锁的占用;而Lock则必须要用户去手动释放锁,如果没有主动释放锁,就有可能导致出现死锁现象。

java.util.concurrent.locks包中常用的类和接口:
(1). Lock

Lock是一个接口:

?
1
2
3
4
5
6
7
8
public interface Lock {
     void lock();
     void lockInterruptibly() throws InterruptedException;
     boolean tryLock();
     boolean tryLock( long time, TimeUnit unit) throws InterruptedException;
     void unlock();
     Condition newCondition();
}
lock()、tryLock()、tryLock(long time, TimeUnit unit)和lockInterruptibly()是用来获取锁的。unLock()方法是用来释放锁的。

在Lock中声明了四个方法来获取锁,那么这四个方法有何区别呢?

<1>.lock():如果锁已被其他线程获取,则进行等待。采用Lock,必须主动去释放锁,并且在发生异常时,不会自动释放锁。因此一般来说,使用Lock必须在try{}catch{}块中进行,并且将释放锁的操作放在finally块中进行,以保证锁一定被被释放,防止死锁的发生。

通常使用Lock来进行同步的话,是以下面这种形式去使用的:

Lock lock = …; lock.lock(); try{ //处理任务 }catch(Exception ex){ }finally{ lock.unlock();//释放锁 } <2>.tryLock():有返回值,表示用来尝试获取锁,如果获取成功,则返回true,如果获取失败(即锁已被其他线程获取),则返回false,也就说这个方法无论如何都会立即返回。在拿不到锁时不会一直在那等待。

tryLock(long time, TimeUnit unit): tryLock()方法是类似的,区别在于这个方法在拿不到锁时会等待一定的时间,在时间期限之内如果还拿不到锁,就返回false。如果一开始拿到锁或者在等待期间内拿到了锁,则返回true。

一般情况下通过tryLock来获取锁时是这样使用的:

Lock lock = …; if(lock.tryLock()) { try{ //处理任务 }catch(Exception ex){ }finally{ lock.unlock();//释放锁 } }else{ //如果不能获取锁,则直接做其他事情 } <3>.lockInterruptibly()

获取锁时,如果线程正在等待获取锁,则这个线程能够响应中断,即中断线程的等待状态。当两个线程同时通过lock.lockInterruptibly()想获取某个锁时,假若此时线程A获取到了锁,而线程B只有在等待,那么对线程B调用thread B.interrupt()方法能够中断线程B的等待过程。

lockInterruptibly()一般的使用形式如下:

publicvoidmethod()throwsInterruptedException { lock.lockInterruptibly(); try{ //….. } finally{ lock.unlock(); } }

 

 注意,当一个线程获取了锁之后,是不会被interrupt()方法中断的。单独调用interrupt()方法不能中断正在运行过程中的线程,只能中断阻塞过程中的线程。

  因此当通过lockInterruptibly()方法获取某个锁时,如果不能获取到,只有进行等待的情况下,是可以响应中断的。

  而用synchronized修饰的话,当一个线程处于等待某个锁的状态,是无法被中断的,只有一直等待下去。

 (2). ReentrantLock

可重入锁。ReentrantLock是唯一实现了Lock接口的类,并且ReentrantLock提供了更多的方法。

例子1,lock()的正确使用方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Test {
     private ArrayList<integer> arrayList = new ArrayList<integer>();
     public static void main(String[] args)  {
         final Test test = new Test();
          
         new Thread(){
             public void run() {
                 test.insert(Thread.currentThread());
             };
         }.start();
          
         new Thread(){
             public void run() {
                 test.insert(Thread.currentThread());
             };
         }.start();
    
      
     public void insert(Thread thread) {
         Lock lock = new ReentrantLock();    //注意这个地方
         lock.lock();
         try {
             System.out.println(thread.getName()+ "得到了锁" );
             for ( int i= 0 ;i< 5 ;i++) {
                 arrayList.add(i);
             }
         } catch (Exception e) {
             // TODO: handle exception
         } finally {
             System.out.println(thread.getName()+ "释放了锁" );
             lock.unlock();
         }
     }
}</integer></integer>

输出结果:
?
1
2
3
4
Thread- 0 得到了锁
Thread- 1 得到了锁
Thread- 0 释放了锁
Thread- 1 释放了锁

 

也许有朋友会问,怎么会输出这个结果?第二个线程怎么会在第一个线程释放锁之前得到了锁?原因在于,在insert方法中的lock变量是局部变量,每个线程执行该方法时都会保存一个副本,那么理所当然每个线程执行到lock.lock()处获取的是不同的锁,所以就不会发生冲突。

  知道了原因改起来就比较容易了,只需要将lock声明为类的属性即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Test {
     private ArrayList<integer> arrayList = new ArrayList<integer>();
     private Lock lock = new ReentrantLock();    //注意这个地方
     public static void main(String[] args)  {
         final Test test = new Test();
          
         new Thread(){
             public void run() {
                 test.insert(Thread.currentThread());
             };
         }.start();
          
         new Thread(){
             public void run() {
                 test.insert(Thread.currentThread());
             };
         }.start();
    
      
     public void insert(Thread thread) {
         lock.lock();
         try {
             System.out.println(thread.getName()+ "得到了锁" );
             for ( int i= 0 ;i< 5 ;i++) {
                 arrayList.add(i);
             }
         } catch (Exception e) {
             // TODO: handle exception
         } finally {
             System.out.println(thread.getName()+ "释放了锁" );
             lock.unlock();
         }
     }
}</integer></integer>
这样就是正确地使用Lock的方法了。

例子2,tryLock()的使用方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Test {
     private ArrayList<integer> arrayList = new ArrayList<integer>();
     private Lock lock = new ReentrantLock();    //注意这个地方
     public static void main(String[] args)  {
         final Test test = new Test();
          
         new Thread(){
             public void run() {
                 test.insert(Thread.currentThread());
             };
         }.start();
          
         new Thread(){
             public void run() {
                 test.insert(Thread.currentThread());
             };
         }.start();
    
      
     public void insert(Thread thread) {
         if (lock.tryLock()) {
             try {
                 System.out.println(thread.getName()+ "得到了锁" );
                 for ( int i= 0 ;i< 5 ;i++) {
                     arrayList.add(i);
                 }
             } catch (Exception e) {
                 // TODO: handle exception
             } finally {
                 System.out.println(thread.getName()+ "释放了锁" );
                 lock.unlock();
             }
         } else {
             System.out.println(thread.getName()+ "获取锁失败" );
         }
     }
}</integer></integer>
输出结果:
?
1
2
3
Thread- 0 得到了锁
Thread- 1 获取锁失败
Thread- 0 释放了锁
例子3,lockInterruptibly()响应中断的使用方法:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Test {
     private Lock lock = new ReentrantLock();  
     public static void main(String[] args)  {
         Test test = new Test();
         MyThread thread1 = new MyThread(test);
         MyThread thread2 = new MyThread(test);
         thread1.start();
         thread2.start();
          
         try {
             Thread.sleep( 2000 );
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         thread2.interrupt();
    
      
     public void insert(Thread thread) throws InterruptedException{
         lock.lockInterruptibly();   //注意,如果需要正确中断等待锁的线程,必须将获取锁放在外面,然后将InterruptedException抛出
         try
             System.out.println(thread.getName()+ "得到了锁" );
             long startTime = System.currentTimeMillis();
             for (    ;     ;) {
                 if (System.currentTimeMillis() - startTime >= Integer.MAX_VALUE)
                     break ;
                 //插入数据
             }
         }
         finally {
             System.out.println(Thread.currentThread().getName()+ "执行finally" );
             lock.unlock();
             System.out.println(thread.getName()+ "释放了锁" );
        
     }
}
  
class MyThread extends Thread {
     private Test test = null ;
     public MyThread(Test test) {
         this .test = test;
     }
     @Override
     public void run() {
          
         try {
             test.insert(Thread.currentThread());
         } catch (InterruptedException e) {
             System.out.println(Thread.currentThread().getName()+ "被中断" );
         }
     }
}
运行之后,发现thread2能够被正确中断。

 

 (3).ReadWriteLock

ReadWriteLock也是一个接口

 

publicinterfaceReadWriteLock { Lock readLock();//获取读锁 Lock writeLock();//获取写锁 }
将文件的读写操作分开,分成2个锁来分配给线程,从而使得多个线程可以同时进行读操作。

下面的ReentrantReadWriteLock实现了ReadWriteLock接口。
(4).ReentrantReadWriteLock

具体用法:有多个线程要同时进行读操作

synchronized达到的效果:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Test {
     private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
      
     public static void main(String[] args)  {
         final Test test = new Test();
          
         new Thread(){
             public void run() {
                 test.get(Thread.currentThread());
             };
         }.start();
          
         new Thread(){
             public void run() {
                 test.get(Thread.currentThread());
             };
         }.start();
          
    
      
     public synchronized void get(Thread thread) {
         long start = System.currentTimeMillis();
         while (System.currentTimeMillis() - start <= 1 ) {
             System.out.println(thread.getName()+ "正在进行读操作" );
         }
         System.out.println(thread.getName()+ "读操作完毕" );
     }
}

这段程序的输出结果是,直到thread1执行完读操作之后,才会打印thread2执行读操作的信息。

 

ReentrantReadWriteLock达到的效果:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Test {
     private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
      
     public static void main(String[] args)  {
         final Test test = new Test();
          
         new Thread(){
             public void run() {
                 test.get(Thread.currentThread());
             };
         }.start();
          
         new Thread(){
             public void run() {
                 test.get(Thread.currentThread());
             };
         }.start();
          
    
      
     public void get(Thread thread) {
         rwl.readLock().lock();
         try {
             long start = System.currentTimeMillis();
              
             while (System.currentTimeMillis() - start <= 1 ) {
                 System.out.println(thread.getName()+ "正在进行读操作" );
             }
             System.out.println(thread.getName()+ "读操作完毕" );
         } finally {
             rwl.readLock().unlock();
         }
     }
}
结果是:thread1和thread2在同时进行读操作。大大提升了读操作的效率

 

 

如果有一个线程已经占用了读锁,则此时其他线程如果要申请写锁,则申请写锁的线程会一直等待释放读锁。

如果有一个线程已经占用了写锁,则此时其他线程如果申请写锁或者读锁,则申请的线程会一直等待释放写锁。

Lock和synchronized的选择:

总结来说,Lock和synchronized有以下几点不同:

  1)Lock是一个接口,而synchronized是Java中的关键字,synchronized是内置的语言实现;

  2)synchronized在发生异常时,会自动释放线程占有的锁,因此不会导致死锁现象发生;而Lock在发生异常时,如果没有主动通过unLock()去释放锁,则很可能造成死锁现象,因此使用Lock时需要在finally块中释放锁;

  3)Lock可以让等待锁的线程响应中断,而synchronized却不行,使用synchronized时,等待的线程会一直等待下去,不能够响应中断;

  4)通过Lock可以知道有没有成功获取锁,而synchronized却无法办到。

  5)Lock可以提高多个线程进行读操作的效率。

  在性能上来说,如果竞争资源不激烈,两者的性能是差不多的,而当竞争资源非常激烈时(即有大量线程同时竞争),此时Lock的性能要远远优于synchronized。所以说,在具体使用时要根据适当情况选择。

锁的相关概念:

1.可重入锁

如果锁具备可重入性,则称作为可重入锁。像synchronized和ReentrantLock都是可重入锁,可重入性实际上表明了锁的分配机制:基于线程的分配,而不是基于方法调用的分配。举个简单的例子,当一个线程执行到某个synchronized方法时,比如说method1,而在method1中会调用另外一个synchronized方法method2,此时线程不必重新去申请锁,而是可以直接执行方法method2。

classMyClass { publicsynchronizedvoidmethod1() { method2(); } publicsynchronizedvoidmethod2() { } }

上述代码中的两个方法method1和method2都用synchronized修饰了,

假如synchronized不具备可重入性,某一时刻,线程A执行到了method1,此时线程A获取了这个对象的锁,而由于method2也是synchronized方法,此时线程A需要重新申请锁。因为线程A已经持有了该对象的锁,而又在申请获取该对象的锁,这样就会线程A一直等待永远不会获取到锁。

  而由于synchronized和Lock都具备可重入性,所以不会发生上述现象。

2.可中断锁

在Java中,synchronized就不是可中断锁,而Lock是可中断锁。

  如果某一线程A正在执行锁中的代码,另一线程B正在等待获取该锁,可能由于等待时间过长,线程B不想等待了,想先处理其他事情,我们可以让它中断自己或者在别的线程中中断它,这种就是可中断锁。

  在前面演示lockInterruptibly()的用法时已经体现了Lock的可中断性。

3.公平锁

公平锁即尽量以请求锁的顺序来获取锁。比如同是有多个线程在等待一个锁,当这个锁被释放时,等待时间最久的线程(最先请求的线程)会获得该所,这种就是公平锁。

  非公平锁即无法保证锁的获取是按照请求锁的顺序进行的。这样就可能导致某个或者一些线程永远获取不到锁。

  在Java中,synchronized就是非公平锁,它无法保证等待的线程获取锁的顺序。

  而对于ReentrantLock和ReentrantReadWriteLock,它默认情况下是非公平锁,但是可以设置为公平锁。

ReentrantLock lock =newReentrantLock(true);//true表示为公平锁,为fasle为非公平锁。默认情况下,如果使用无参构造器,则是非公平锁。

另外在ReentrantLock类中定义了很多方法,比如:

  isFair() //判断锁是否是公平锁

  isLocked() //判断锁是否被任何线程获取了

  isHeldByCurrentThread() //判断锁是否被当前线程获取了

  hasQueuedThreads() //判断是否有线程在等待该锁

  在ReentrantReadWriteLock中也有类似的方法,同样也可以设置为公平锁和非公平锁。

不过要记住,ReentrantReadWriteLock并未实现Lock接口,它实现的是ReadWriteLock接口。

4.读写锁

读写锁将对一个资源(比如文件)的访问分成了2个锁,一个读锁和一个写锁。

  正因为有了读写锁,才使得多个线程之间的读操作不会发生冲突。

  ReadWriteLock就是读写锁,它是一个接口,ReentrantReadWriteLock实现了这个接口。

  可以通过readLock()获取读锁,通过writeLock()获取写锁。

点击复制链接 与好友分享! 回本站首页
上一篇: Java注解Annotation浅析
下一篇: SpringMVC + MyBatis + Ajax+验证码
相关文章
图文推荐
收藏文章
有事没事说两句…
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后…
取消上传
评论内容为空!
            <!-- 放置cbox发布状态 -->
            <!-- 提示条 -->
            <!-- 零评论提示条 -->
            <div class="list-comment-empty-w">
                <div node-type="empty-prompt" class="empty-prompt-w">
                    <span class="prompt-null-w">还没有评论,快来抢沙发吧!</span>
                </div>
            </div>
            <!-- 提示连接到快站社区 -->
            <!-- <div class="list-comment-kuaizhan-w">
                <div node-type="kuaizhan-prompt" class="kuaizhan-prompt-w">
                    <span class="prompt-text-w">点击查看更多精彩内容</span>
                </div>
            </div> -->
            <!--关闭评论-->
            <div class="list-close-comment-w">

            </div>
        </div>
    </div>
</div>

  • 最新评论

        红黑联盟正在使用畅言
                    </a>
    </div>
</div>
<div node-type="cy-to-shequ" class="cy-redirect-btn">
    <span class="cy-redirect-text">去社区看看吧</span><i class="cy-right-arrow"></i>
</div>
<div node-type="cy-to-hots" class="cy-redirect-btn">
    <span class="cy-redirect-text">去热评看看吧</span><i class="cy-right-arrow"></i>
</div>
<div class="cy-to-shequ-float"></div>


  •     <li node-type="notice-message" data-alias="message" data-type="message" data-static="static" class="nt-item" style=" display: none ">
            <div class="nt-logo"></div>
            <a node-type="notice-content" class="nt-text" href="javascript:void(0);">你收到<i>0</i>条新通知</a>
            <a class="nt-close" href="javascript:void(0);"></a>
        </li>
    
        <li node-type="notice-support" data-alias="support" data-type="support" data-static="static" class="nt-item" style=" display: none ">
            <div class="nt-logo"></div>
            <a node-type="notice-content" class="nt-text" href="javascript:void(0);">你有<i>0</i>条评论收到赞同</a>
            <a class="nt-close" href="javascript:void(0);"></a>
        </li>
    
        <li node-type="notice-reply" data-alias="reply" data-type="reply" data-static="static" class="nt-item" style=" display: none ">
            <div class="nt-logo"></div>
            <a node-type="notice-content" class="nt-text" href="javascript:void(0);">你有<i>0</i>条新回复</a>
            <a class="nt-close" href="javascript:void(0);"></a>
        </li>
    
        <li node-type="notice-hots" data-alias="hots" data-type="hots" data-static="static" class="nt-item" style=" display: none ">
            <div class="nt-logo"></div>
            <a node-type="notice-content" class="nt-text" href="javascript:void(0);">本日畅言热评新鲜出炉啦!</a>
            <a class="nt-close" href="javascript:void(0);"></a>
        </li>
    
        <li node-type="notice-task" data-alias="task" data-type="task" data-static="static" class="nt-item" style=" display: none ">
            <div class="nt-logo"></div>
            <a node-type="notice-content" class="nt-text" href="javascript:void(0);">你有<i>0</i>个任务已完成</a>
            <a class="nt-close" href="javascript:void(0);"></a>
        </li>
    
        <li node-type="notice-history" data-alias="history" data-type="history" data-static="static" class="nt-item" style=" display: none ">
            <div class="nt-logo"></div>
            <a node-type="notice-content" class="nt-text" href="javascript:void(0);">你收获<i>0</i>个畅言足迹</a>
            <a class="nt-close" href="javascript:void(0);"></a>
        </li>
            </ul>
    






    </div>
    <div class="box_right_box mt8">
        <dl class="bTitle tabtitle borT" id="ltabTitle"><dd class="on">文章</dd><dd>推荐</dd></dl>
            <div class="paddingbox tab_wrap" id="listTab" style="height: 192px;">
            <div class="tab_main" style="width: 556px; height: 192px;">
            <dl class="index tab_item swiper-slide-visible swiper-slide-active" style="width: 278px; height: 192px;">
                                                        <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/article/201305/211463.html">网络安全之Tomcat访问日志删不掉,怎么</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/article/201211/168950.html">XSS绕过技术</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/article/200906/38993.html">网站进行入侵检测时网站后台上传不上去</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/article/201011/79018.html">织梦DEDECMS补丁后的getshell</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/article/200903/36653.html">基于XSRF的SQL注入技术</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/article/201112/114459.html">DedeCMS缺陷导致数据库插入一句话木马</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/article/201002/44694.html">PHPCMS 2007 / 2008 跨站脚本(XS</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/article/201308/232922.html">wscript.shell组件被禁用时 可以用Sh</a></dd>
                                                    </dl>
            <dl class="index tab_item" style="width: 278px; height: 192px;">
                                                        <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/special/win7jh/">win7激活工具</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/os/201607/522176.html">win10激活工具</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/special/win7jihuogongju/">win7激活工具旗舰版</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/os/201602/489186.html">office2010激活密钥</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/os/201601/456235.html">windows7激活密钥</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/os/201601/488006.html">office2010激活工具</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/os/201601/456247.html">小马激活工具</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/os/201607/524552.html">win10激活工具</a></dd>
                                                    </dl>
            </div>
        </div>
    </div>

    <div class="side-ibox mt8">
    <!-- 2cto_右二 -->
    <script type="text/javascript">cto_A_D("2cto_7");</script><div style=""><iframe width="300" frameborder="0" height="250" scrolling="no" src="https://pos.baidu.com/s?hei=250&amp;wid=300&amp;di=u2597730&amp;ltu=https%3A%2F%2Fwww.2cto.com%2Fkf%2F201609%2F548244.html&amp;pcs=1519x734&amp;ti=java%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8-%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%AD%A5-synchronized%E5%92%8Clock%20-%20JAVA%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0%20-%20%E7%BA%A2%E9%BB%91&amp;cja=false&amp;exps=111000&amp;ps=867x949&amp;par=1536x824&amp;cfv=0&amp;cmi=5&amp;tpr=1528984387688&amp;ari=2&amp;psr=1536x864&amp;pss=1519x18759&amp;chi=1&amp;tcn=1528984388&amp;drs=1&amp;cce=true&amp;dtm=HTML_POST&amp;dc=3&amp;ccd=24&amp;pis=-1x-1&amp;dai=7&amp;tlm=1528984388&amp;ant=0&amp;prot=2&amp;cec=GBK&amp;cdo=-1&amp;cpl=4&amp;col=zh-CN&amp;ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DIPZDaQ1DmY1yiOqxQedWpL89CMZiqLXjiSuhxYAZ2_ozPyEAFCHRFhki0gMPZ9XS9kXT0gWFsUa5yimtMMgdRK%26wd%3D%26eqid%3Db0646f23000038bc000000065b227322&amp;dri=0&amp;dis=0"></iframe><em style="width:0px;height:0px;"></em></div><script type="text/javascript" src="//daima.dsxdn.com/sf3a1ec299f0cdf438db14798aafed22e01be6ce1c3fec7fe1103c.js"></script>

    </div>

    <div class="box_right_box mt8">
        <dl class="bTitle tabtitle borT"><dd class="on">点击排行</dd></dl>
        <div class="paddingbox">
            <dl class="index">
                <dd class="picline"></dd>
                                                        <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/kf/201606/518341.html">手把手教你整合最优雅SSM框架:Spring</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/kf/201311/260815.html">java中long类型转换为int类型</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/kf/201605/510933.html">java读写excel(POI,支持xls和xlsx两</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/kf/201403/286536.html">Java ConcurrentModificationExcepti</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/kf/201504/389182.html">Spring的refresh()方法相关异常</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/kf/201501/369369.html">Java利用POI实现数据的Excel导出</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/kf/201210/161016.html">StringUtils中 isNotEmpty 和isNotB</a></dd>
                                    <dd class="list">·&nbsp;<a target="_blank" href="https://www.2cto.com/kf/201503/384969.html">java中Map和List初始化的两种方法</a></dd>
                                                    </dl>
        </div>
    </div>
    <div class="side-ibox mt8">
    <!-- 2cto_右三 -->
       <script type="text/javascript">cto_A_D("2cto_14");</script><div style="width: 100%;"><iframe width="300" frameborder="0" height="250" scrolling="no" src="//pos.baidu.com/s?hei=250&amp;wid=300&amp;di=u2597736&amp;ltu=https%3A%2F%2Fwww.2cto.com%2Fkf%2F201609%2F548244.html&amp;cpl=4&amp;ari=2&amp;drs=1&amp;cdo=-1&amp;dis=0&amp;pis=-1x-1&amp;ps=1371x949&amp;dtm=HTML_POST&amp;cmi=5&amp;tpr=1528984387688&amp;dri=0&amp;ant=0&amp;par=1536x824&amp;pcs=1519x734&amp;dc=3&amp;cec=GBK&amp;cja=false&amp;chi=1&amp;cfv=0&amp;ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DIPZDaQ1DmY1yiOqxQedWpL89CMZiqLXjiSuhxYAZ2_ozPyEAFCHRFhki0gMPZ9XS9kXT0gWFsUa5yimtMMgdRK%26wd%3D%26eqid%3Db0646f23000038bc000000065b227322&amp;tlm=1528984388&amp;exps=111000&amp;tcn=1528984388&amp;psr=1536x864&amp;dai=8&amp;cce=true&amp;col=zh-CN&amp;prot=2&amp;ccd=24&amp;pss=1519x18759&amp;ti=java%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8-%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%AD%A5-synchronized%E5%92%8Clock%20-%20JAVA%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0%20-%20%E7%BA%A2%E9%BB%91"></iframe></div><script type="text/javascript" src="//daima.dsxdn.com/yl3a1ec299f0cdf43edb14798aafed22e01be6ce1c3fec7fe1103c.js"></script>

    </div>
    <div class="box_right_box mt8">
        <div class="paddingbox">
            <!-- <div id="cyHotnews" role="cylabs" data-use="hotnews"></div>
            <script type="text/javascript" charset="utf-8" src="//changyan.itc.cn/js/??lib/jquery.js,changyan.labs.js?appid=cyrBEfE7C"></script> -->
        </div>
    </div>
    <div class="side-ibox mt8">
        <!-- 2cto_右四 -->
        <script type="text/javascript">cto_A_D("2cto_8");</script><div id="_e55exqlnbrg" style="width: 300px; height: 250px; display: inline-block;"><iframe id="iframeu2597743_0" name="iframeu2597743_0" src="https://pos.baidu.com/ncrm?conwid=300&amp;conhei=250&amp;rdid=2597743&amp;dc=3&amp;di=u2597743&amp;dri=0&amp;dis=0&amp;dai=9&amp;ps=1651x949&amp;enu=encoding&amp;dcb=___adblockplus&amp;dtm=HTML_POST&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=0&amp;tpr=1528984388296&amp;ti=java%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8-%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%AD%A5-synchronized%E5%92%8Clock%20-%20JAVA%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0%20-%20%E7%BA%A2%E9%BB%91&amp;ari=2&amp;dbv=2&amp;drs=1&amp;pcs=1519x734&amp;pss=1519x18775&amp;cfv=0&amp;cpl=4&amp;chi=1&amp;cce=true&amp;cec=GBK&amp;tlm=1528984388&amp;prot=2&amp;rw=734&amp;ltu=https%3A%2F%2Fwww.2cto.com%2Fkf%2F201609%2F548244.html&amp;ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DIPZDaQ1DmY1yiOqxQedWpL89CMZiqLXjiSuhxYAZ2_ozPyEAFCHRFhki0gMPZ9XS9kXT0gWFsUa5yimtMMgdRK%26wd%3D%26eqid%3Db0646f23000038bc000000065b227322&amp;ecd=1&amp;uc=1536x824&amp;pis=-1x-1&amp;sr=1536x864&amp;tcn=1528984388&amp;qn=ef464f9f9023cfbf&amp;tt=1528984388281.19.85.235" width="300" height="250" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" style="border:0;vertical-align:bottom;margin:0;width:300px;height:250px" allowtransparency="true"></iframe></div><script type="text/javascript" src="//daima.dsxdn.com/fs3a1ec299f0cdf33bdb14798aafed22e01be6ce1c3fec7fe1103c.js"></script>

    </div>
</div><!--box_right  end-->

(function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟–致力于做实用的IT技术学习网站

var cpro_id = 'u2915514';

a

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值