什么是多线程的滑动条件?

Slipped Condition是一种特殊类型的竞争条件,可以在多线程应用程序中发生。在此,线程在读取条件之后执行与之相关的活动之前被暂停。它很少发生,但是,如果结果不符合预期,就必须寻找它。

例如:假设有两个线程A和线程B这要处理一个串S。首先,启动线程A,它检查是否还有剩余要处理的字符,最初整个字符串可用于处理,因此条件为真。现在,线程A被挂起并且线程B开始。它再次检查条件,其计算结果为true,然后处理整个字符串S.现在,当线程A再次开始执行时,字符串S在此时被完全处理,因此发生错误。这被称为滑倒条件。

publicclassMain { publicstaticvoidmain(String[] args) { ReadingThread readingThread = newReadingThread(); SlippedThread slippedThread = newSlippedThread(); slippedThread.start(); readingThread.start(); } } classCommonResource { staticfinalString string = "Hello"; staticintpointerPosition = 0; } // Thread to demonstrate a slipped condition classSlippedThread extendsThread { @Overridepublicvoidrun() { // Check if any characters // are left to process if(CommonResource.pointerPosition != CommonResource.string.length()) { System.out.println("Characters left! "+ "I can process the string"); // Cause the thread to wait to cause // a slipped condition try{ synchronized(this) { wait(500); } } catch(InterruptedException e) { System.out.println(e); } try{ while(CommonResource.pointerPosition < CommonResource.string.length()) { System.out.println("Thread1 "+ CommonResource.string .charAt( CommonResource .pointerPosition)); CommonResource.pointerPosition++; } } catch(StringIndexOutOfBoundsException e) { System.out.println("\nNo more character left"+ " to process. This is a"+ " slipped condition"); } } } } // Thread to process the whole String classReadingThread extendsThread { @Overridepublicvoidrun() { System.out.println("Thread2 trying to "+ "process the string"); while(CommonResource.pointerPosition < CommonResource.string.length()) { System.out.print("Thread2 "+ CommonResource.string .charAt( CommonResource .pointerPosition)); CommonResource.pointerPosition++; } } }

输出:

剩余字符!我可以处理字符串
Thread2试图处理字符串
Thread2 H.
Thread2 e
Thread2 l
Thread2 l
Thread2 o
没有更多的角色可以处理了。这是一个滑坡的条件

Slipped Conditions问题的解决方案非常简单明了。检查条件后线程将要访问的任何资源必须由线程锁定,并且只应在线程执行工作后释放。必须同步所有访问权限

对于上面的问题,可以通过锁定CommonResource类的String对象来消除滑动条件。在这种情况下,线程首先获得访问权并锁定String,然后尝试处理String。

publicclassMain { publicstaticvoidmain(String[] args) { ReadingThread readingThread = newReadingThread(); SlippedThread slippedThread = newSlippedThread(); slippedThread.start(); readingThread.start(); } } classCommonResource { staticfinalString string = "Hello"; staticintpointerPosition = 0; // A static variable added // to lock the String object staticbooleanisLocked = false; } // Thread to demonstrate a slipped condition classSlippedThread extendsThread { @Overridepublicvoidrun() { // Check if any characters // are left to process if(CommonResource.isLocked != true&& CommonResource.pointerPosition != CommonResource.string.length()) { System.out.println("Characters left! "+ "I can process the string"); CommonResource.isLocked = true; // Cause the thread to wait to cause // a slipped condition try{ synchronized(this) { wait(250); } } catch(InterruptedException e) { System.out.println(e); } try{ while(CommonResource.pointerPosition < CommonResource.string.length()) { System.out.println("Thread1 "+ CommonResource .string.charAt( CommonResource .pointerPosition)); CommonResource.pointerPosition++; } } catch(StringIndexOutOfBoundsException e) { System.out.println("\nNo more character "+ "left to process. This is "+ "a slipped condition"); } CommonResource.isLocked = false; } } } // Thread to process the whole String classReadingThread extendsThread { @Overridepublicvoidrun() { System.out.println("Thread2 trying to"+ " process the string"); if(CommonResource.isLocked == false) { CommonResource.isLocked = true; synchronized(this) { while(CommonResource.pointerPosition < CommonResource.string.length()) { System.out.println("Thread2 "+ CommonResource.string .charAt( CommonResource .pointerPosition)); CommonResource.pointerPosition++; } } } CommonResource.isLocked = false; } }

产量

剩余字符!我可以处理字符串
Thread2试图处理字符串
Thread1 H.
Thread1 e
Thread1 l
Thread1 l
Thread1 o

在上面的程序中,一个新的静态布尔成员isLocked被添加到CommonResource类中。现在,每当线程尝试处理字符串时,它首先获取锁定然后对其进行处理。在这个例子中,SlippedCondition线程检查是否isLocked为假且有串留待处理。如果是,则获取锁定然后等待并最终处理该字符串。同时,ReadingThread尝试处理字符串,但它不能作为isLocked被设置为true SlippedThread

分享到:

发表评论

昵称

沙发空缺中,还不快抢~