導航:首頁 > 編程語言 > javaviolate關鍵字

javaviolate關鍵字

發布時間:2023-03-17 19:27:47

java.lang.IllegalArgumentException: Comparison method violates its general contract!

System.setProperty("雹亂攜java.util.Arrays.useLegacyMergeSort"源伏, "true"陪鋒);

http://www.tuicool.com/articles/MZreyuv

⑵ java中的int類型的線程安全,AtomicInteger和violate

AtomicInteger,弊廳一個提供原子操作的Integer的類。
在賀巧Java語言中,++i和i++操作並不是線程安全的。在使用的時候,不可避免的會用到synchronized關鍵字。而AtomicInteger則通過一種線程安全的加減操作介面。
而volatile修飾,只是保證每次取a的值都不是從緩存中取,而是從a所真正對應的內存地址中取租拍隱

⑶ java中的int類型的線程安全,AtomicInteger和violate

AtomicInteger,一個提供原子操作的Integer的類。

在Java語言中,++i和i++操作並不是線程安全的。在使回用的時候,不可避免的會答用到synchronized關鍵字。而AtomicInteger則通過一種線程安全的加減操作介面。

而volatile修飾,只是保證每次取a的值都不是從緩存中取,而是從a所真正對應的內存地址中取

⑷ violate關鍵字的含義

你是要說volatile
一個定義為volatile的變數是說這變數可能會被意想不到地改變,這樣,編譯器就不會去假設這個變數的值了。精確地說就是,優化器在用到這個變數時必須每次都小心地重新讀取這個變數的值(From Memory),而不是使用保存在寄存器里的備份。
下面是volatile變數的幾個例子:
1) 並行設備的硬體寄存器(如:狀態寄存器)
2) 一個中斷服務子程序中會訪問到的非自動變數(Non-automatic variables)
3) 多線程應用中被幾個任務共享的變數

這個多用在嵌入式開發中,一般場合不需要使用。

⑸ 用java實現野人傳教士過河問題




解題時可以把渡河的過程省略掉,也就是可以忽略河的存在。
為了用 Java 解這道題,我寫了 3 個類:
河邊(RiverSide)、河景(RiverScene)和解題者(MACPS,即 Missionaries And Cannibals Puzzle Solver)。

問題中的傳教士、野人和船都是非常簡單的物件,所以就簡單地用單字元字元串「m」、「c」 和 「v」來代表。

其實那三個類都很簡單;它們之間的關系也很簡單:
1)每個河邊都能有 0 或更多的 m 和 c,及最多 1 個 v。
2)每個河景里有兩個河邊。
3)解題者里有兩個河景:初始河景和終極河景。

解題者的任務就是要搜索出能把初始河景及終極河景連起來的河景系列。
解題者的 getSolutionSteps( )以遞歸的方式作深度優先搜索。

其它兩個「類」(Combinatorics 和 Copy)都是只提供一個方法的簡單類。

代碼里有文檔。 有不明白的地方可以問。
你可以用類似 Jacobe (http://www.tiobe.com/jacobe.htm)的程序恢復代碼原有的縮進。

import java.util.*;
import java.io.*;

/**
* Missionaries And Cannibals Puzzle Solver.
*
* Solution to the puzzle must not violate any of the following 2 rules:
* 1) Relative headcount: cannibals can't outnumber missionaries at any point.
* 2) Boat's loading: mustn't exceed the max.
*/
public class MACPS {
public class SolutionNotFoundException extends RuntimeException { }

static final Object MISSIONARY = "m", // Simple representation
CANNIBAL = "c", // of objects
BOAT = "v"; // in the puzzle.
private int boat_max_load,
boat_min_load = 1; // Shouldn't be any other value.
private RiverScene firstScene,
finalScene;

// Recursively searches for a solution using Depth-First Search strategy.
// Takes a Stack containing only the first scene (root of tree).
// Returns a collection of scenes connecting the first scene to the final.
// Throws SolutionNotFoundException for obvious reason.
// Deploys the following optimization strategy:
// Transfers as much as possible from source side,
// as little as possible from target side.
private Collection getSolutionSteps( Stack takenSteps ) {
RiverScene lastScene = ( RiverScene ) takenSteps.peek( );
if( lastScene.equals( finalScene ) ) return takenSteps;

RiverScene newStep = lastScene.deepCopy( );

// To allow transfer in both directions to share the same chunk of code.
int start = boat_max_load,
stop = boat_min_load - 1,
step = -1;
RiverSide from = newStep.lside,
to = newStep.rside;
if( to.hasBoat( ) ) {
start = boat_min_load;
stop = boat_max_load + 1;
step = 1;
from = newStep.rside;
to = newStep.lside;
}

for( int nPassenger = start; nPassenger != stop; nPassenger += step ) {
Collection menCombs = new HashSet( // HashSet eliminates plicates.
Combinatorics.combinations( from.getMenList( ), nPassenger ) );

nextComb:
for( Iterator comb = menCombs.iterator( ); comb.hasNext( ); ) {
Collection menList = ( Collection ) comb.next( );
try {
from.transferMen( to, menList );

// If it's a taken step, undo and try next combination.
for( Iterator i = takenSteps.iterator( ); i.hasNext( ); )
if( i.next( ).equals( newStep ) ) {
to.transferMen( from, menList );
continue nextComb;
}

takenSteps.push( newStep );
return getSolutionSteps( takenSteps );
}
catch( SecurityException e ) {
// Transfer didn't take place. Just try next combination.
}
catch( SolutionNotFoundException e ) {
// New step led to no solution in leaves. Undo, then next.
takenSteps.pop( );
to.transferMen( from, menList );
}
}
}
// All possible steps led to no solution, so
throw new SolutionNotFoundException( );
}

// Do setup, then kick-starts getSolutionSteps( Stack takenSteps ).
public Collection
getSolutionSteps( int nMissionary, int nCannibal, int boatCapacity ) {
if( nMissionary < 0 || nCannibal < 0 || boatCapacity < 0 )
throw new IllegalArgumentException( "Negative argument value." );

RiverSide sourceSide = new RiverSide( nMissionary, nCannibal, true ),
targetSide = new RiverSide( 0, 0, false );
boat_max_load = boatCapacity;
firstScene = new RiverScene( sourceSide, targetSide );
finalScene = new RiverScene( targetSide, sourceSide );

if( firstScene.lside.fatal( ) ) // First scene can be valid but fatal.
throw new SolutionNotFoundException( );

Stack steps = new Stack( );
steps.push( firstScene );
return getSolutionSteps( steps );
}

public static void main( String[ ] args ) {
int nMissionary = 3,
nCannibal = 3,
boatCapacity = 2;

System.out.println(
"\nSolving the puzzle of Missionaries And Cannibals with\n" +
nMissionary + " missionaries and " + nCannibal + " cannibals " +
"and a boat that can take up to " + boatCapacity + " creatures.." );

try {
Collection steps = new MACPS( ).
getSolutionSteps( nMissionary, nCannibal, boatCapacity );
System.out.println( "\nSolution found:\n" );
for( Iterator step = steps.iterator( ); step.hasNext( ); )
System.out.println( step.next( ) + "\n" );
}
catch( SolutionNotFoundException e ) {
System.out.println( "\nNo solution found." );
}
}
}

/**
* Represents a riverside in the puzzle.
*/
class RiverSide implements Serializable {
private ArrayList men = new ArrayList( ),
boat = new ArrayList( );

public RiverSide( int nMissionary, int nCannibal, boolean withBoat ) {
men.addAll( Collections.nCopies( nMissionary, MACPS.MISSIONARY ) );
men.addAll( Collections.nCopies( nCannibal, MACPS.CANNIBAL ) );
Collections.sort( men );
if( withBoat ) boat.add( MACPS.BOAT );
}

public RiverSide deepCopy( ) {
return ( RiverSide ) Copy.deepCopy( this );
}

public Collection getMenList( ) {
return ( Collection ) Copy.deepCopy( men );
}

public boolean equals( Object otherSide ) {
RiverSide other = ( RiverSide ) otherSide;
Collections.sort( men );
Collections.sort( other.men );
return this.men.equals( other.men ) && this.boat.equals( other.boat );
}

public String toString( ) {
return "BOAT" + boat + "\t" + "MEN" + men;
}

public boolean hasBoat( ) {
return ! boat.isEmpty( );
}

// Checks for violation of Rule #1.
public boolean fatal( ) {
int mCount = 0, cCount = 0;
for( Iterator i = men.iterator( ); i.hasNext( ); ) {
Object val = i.next( );
if( val.equals( MACPS.MISSIONARY ) ) ++mCount;
if( val.equals( MACPS.CANNIBAL ) ) ++cCount;
}
return mCount > 0 && mCount < cCount;
}

// Throws SecurityException if the transfer of all men in menList
// from this to destination *will* result in violation of Rule #1.
// Else, executes the transfer.
public void transferMen( RiverSide destination, Collection menList ) {
for( Iterator i = menList.iterator( ); i.hasNext( ); )
destination.men.add( men.remove( men.indexOf( i.next( ) ) ) );

// A nice place to automate boat transfer.
_transferBoat( destination );

// Undo the transfer if it led to violation of Rule #1.
if( fatal( ) || destination.fatal( ) ) {
destination.transferMen( this, menList );
throw new SecurityException( );
}
}

// Tansfers boat from this to destination. Called only by transferMen( ).
private void _transferBoat( RiverSide destination ) {
destination.boat.add( boat.remove( 0 ) );
}
}

/**
* Combines two riversides. Serves mainly as a data object.
*/
class RiverScene implements Serializable {
RiverSide lside, rside; // Package access.

public RiverScene( RiverSide lside, RiverSide rside ) {
this.lside = lside.deepCopy( );
this.rside = rside.deepCopy( );
}

public RiverScene deepCopy( ) {
return ( RiverScene ) Copy.deepCopy( this );
}

public boolean equals( Object otherScene ) {
RiverScene other = ( RiverScene ) otherScene;
return lside.equals( other.lside ) && rside.equals( other.rside );
}

public String toString( ) {
return "Left Side:\t" + lside + "\n" + "Right Side:\t" + rside;
}
}

/**
* Provides a static method to generate combinations of items taken r at a time.
*/
class Combinatorics {
public static Collection combinations( Collection items, int r ) {
if( r == 0 ) // Return [ [ ] ]. Note that [ ] denotes a List.
return Collections.nCopies( 1, new ArrayList( ) );

List = new ArrayList( items ), // To enable subListing of items.
result = new ArrayList( );
for( int i = 0; i < .size( ); ++i ) {
Collection subCombs =
combinations( .subList( i + 1, .size( ) ), r - 1 );
for( Iterator iter = subCombs.iterator( ); iter.hasNext( ); ) {
// Assign [ [ items.get( i ) ] ] to subComb.
List subComb = new ArrayList( .subList( i, i + 1 ) );

subComb.addAll( ( List ) iter.next( ) );
result.add( subComb );
}
}
return result;
}
}

/**
* Provides a static method to perform deep of object via serialization.
*/
class Copy {
public static Object deepCopy( Object o ) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream( );
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( o );
oos.close( );

ObjectInputStream ois =
new ObjectInputStream(
new ByteArrayInputStream( baos.toByteArray( ) ) );
return ois.readObject( );
}
catch ( Exception e ) {
throw new RuntimeException( e );
}
}
}


閱讀全文

與javaviolate關鍵字相關的資料

熱點內容
編程怎麼與steam教育融合 瀏覽:697
js製作滑鼠拖拽小塊 瀏覽:310
將圖紙拆分為多個CAD文件 瀏覽:779
如何鑒別dsd文件 瀏覽:902
thinkphp不能用js 瀏覽:664
蘋果11粘膩app是什麼意思 瀏覽:670
安卓手機中木馬了怎麼辦 瀏覽:964
java組建模型 瀏覽:53
wifi萬能密碼安全嗎 瀏覽:785
紅色系圓圈是什麼app 瀏覽:714
迷你編程開始的教程怎麼過 瀏覽:216
上海國衡網站有什麼用 瀏覽:29
掃描文件如何全選 瀏覽:363
directx一鍵修復工具 瀏覽:620
如何恢復谷歌同步中刪除的文件夾 瀏覽:215
安卓51轉換為系統應用 瀏覽:789
哪裡看雙11數據 瀏覽:783
文件變成exe如何恢復 瀏覽:49
為什麼逆戰會缺少文件 瀏覽:180
蘋果4s版本怎麼更新 瀏覽:418

友情鏈接