Position:Home>JAVA Development> JAVA tutorial: The multi-line Cheng mechanism of analytic Java
JAVA tutorial: The multi-line Cheng mechanism of analytic Java
From;  Author:Stand originally

  
How to solve this kind of problem? Very simple, in RegisterAccount kind the key word Synchronized that is used at label synchronism method is added in methodological definition. Such, resource is shared in what this method involves in synchronous method executive process (FBalance member variable is in afore-mentioned code) will be added share a lock, share resource in order to ensure only during the method moves this method can is opposite to have a visit, till the line Cheng of this method moving end is opened share a lock, ability of other line Cheng visits these share resource quite. When sharing a lock to was not opened other visits the line Cheng that shares resource to be in block condition.
  
The RegisterAccount after undertaking line Cheng synchronous strategy controls kind define face code place as follows to show:
  
Public Class RegisterAccount
{
Float FBalance;
Public Synchronized Void Deposit(float FFees){FBalance = FFees; }
Public Synchronized Void Withdraw(float FFees){FBalance -= FFees; }
  ……
}

  
The code form after from synchronism of course line Cheng the mechanism defines can see: The key word of methodological visit attribute that having a visit to sharing resource (Public) hind add synchronism defines key word Synchronized, make when synchronous method is being visited to sharing resource, add for these sensitive resource the resource during sharing a lock to control a method to carry out is exclusive quality, the consistency that realized resource of applied system data manages and safeguard.


5, the management of Java line Cheng
  
  
The condition of line Cheng is controlled
  
What need to make clear here is: No matter use successive Thread kind the multi-line Cheng ability that still realizes Runnable interface to implement applied program, the definition in needing to be in this kinds is used at finishing the Run method of real function, this Run method calls line Cheng system (Thread Body) . According to line Cheng body the condition in memory of all of computer science department differs, can be line Cheng cent found, be in order, move, Morpheus, hang wait for a type with death. Types of these line Cheng condition get offline the feature of Cheng is:
  
Establish position: After using New key word to found example of line Cheng object, it exists as an object example merely, JVM did not allot CPU time for its piece Cheng waiting for a line runs resource;
  
Condition of be in order: Start method is called to be the state transition of line Cheng condition of be in order in be in the line Cheng that establishs position. At this moment, line Cheng has gotten the other system resource except CPU time, the line Cheng that waits for JVM only attempers implement be opposite according to the first step of line Cheng this line Cheng undertakes attempering, make this line Cheng is had thereby can win CPU time piece opportunity.
Previous 1 2 3 4 56 7 8 Next