Last active
August 29, 2018 20:24
-
-
Save gamezcua1/97b2946c1377590f4d300fad04fc59ed to your computer and use it in GitHub Desktop.
A simple demostration of the System class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Properties; | |
import java.lang.*; | |
import java.io.*; | |
class Obj { | |
static boolean recicladorActivo = false; | |
static boolean flagSalida = false; | |
static int objCreados = 0; | |
static int objFinalizados = 0; | |
static int objFinalizadosConSalidaATrue = 0; | |
int numObjetos; | |
int arrayEnteros[]; // Array utiliza para consumir memoria | |
Obj( int contador ) { | |
arrayEnteros = new int[500]; | |
numObjetos = contador; | |
objCreados++; | |
// La siguiente sentencia indica cuando se crea el objeto #1000 | |
if( numObjetos == 1000 ) | |
System.out.println( "Creado el objeto #1000" ); | |
} | |
protected void finalize() { | |
objFinalizados++; | |
if( !recicladorActivo ) { | |
recicladorActivo = true; | |
System.out.println( | |
"Iniciado el reciclado en el Objeto numero " | |
+ numObjetos ); | |
} | |
if( numObjetos == 1000 ) { | |
System.out.println( | |
"Finalizado el objeto #1000. Fija la salida a true" ); | |
flagSalida = true; | |
} | |
if( flagSalida ) | |
objFinalizadosConSalidaATrue++; | |
} | |
} | |
public class Team { | |
public static void arrayCopyDemo(){ | |
System.out.println("System.arrayCopy Demo"); | |
int arr1[] = { 0, 1, 2, 3, 4, 5 }; | |
int arr2[] = { 5, 10, 20, 30, 40, 50 }; | |
// copies an array from the specified source array | |
System.arraycopy(arr1, 0, arr2, 0, 1); | |
System.out.print("array2 = "); | |
System.out.print(arr2[0] + " "); | |
System.out.print(arr2[1] + " "); | |
System.out.print(arr2[2] + " "); | |
System.out.print(arr2[3] + " "); | |
System.out.print(arr2[4] + " \n"); | |
System.out.println("========================"); | |
} | |
public static void hasCodeDemo(){ | |
System.out.println("\n========================"); | |
System.out.println("System.identityHashCode()"); | |
File filename1 = new File("hola"); | |
File filename2 = new File("jamas"); | |
File filename3 = new File("s12"); | |
int returnvalue1 = System.identityHashCode(filename1); | |
System.out.println(returnvalue1); | |
int returnvalue2 = System.identityHashCode(filename2); | |
System.out.println(returnvalue2); | |
int returnvalue3 = System.identityHashCode(filename3); | |
System.out.println(returnvalue3); | |
} | |
public static void loadLibraryDemo(){ | |
System.out.println("\n========================"); | |
System.out.println("System.loadLibrary()"); | |
// load a library that is Windows/System32 folder | |
System.out.println("Loading Library..."); | |
try { | |
Runtime.getRuntime().loadLibrary("C:/Windows/System32/crypt32.dll"); | |
System.out.println("Library Loaded."); | |
} catch (UnsatisfiedLinkError e) { | |
System.out.println("Library was not found"); | |
} | |
} | |
volatile static int objContador = 0; | |
public static void finalizationDemo() { | |
System.out.println("\n========================"); | |
System.out.println("System.runFinalization()"); | |
while( objContador < 1200 ) { | |
// Solicita el reciclado cada 120 objetos | |
if( (objContador % 120 ) == 0 ) System.gc(); | |
new Obj( objContador++ ); | |
} | |
System.out.println( "Objetos totales creados = " + Obj.objCreados ); | |
System.out.println( "Objetos totales finalizados = " + Obj.objFinalizados ); | |
// Intenta forzar la finalizacion de los objetos con "runFinalization()" | |
System.out.println( "Intenta forzar la finalizacion." ); | |
System.runFinalization(); | |
System.out.println( "Objetos totales finalizados = " + Obj.objFinalizados ); | |
} | |
public static void propertiesDemo(){ | |
System.out.println("\n========================"); | |
System.out.println("System.getProperties()"); | |
try { | |
// selecciona el nuevo objeto propierties a partir de "misPropiedades.txt" | |
FileInputStream propFile = new FileInputStream("misPropiedades.txt"); | |
Properties p = new Properties(System.getProperties()); | |
p.load(propFile); | |
// selecciona las propiedades del sistema | |
System.setProperties(p); | |
System.getProperties().list(System.out); // selecciona las nuevas propiedades | |
} catch (java.io.FileNotFoundException e) { | |
System.err.println("No puedo encontrar misPropiedades.txt."); | |
} catch (java.io.IOException e) { | |
System.err.println("I/O failed."); | |
} | |
} | |
public static void main( String[] args ){ | |
System.out.println("\nJava.lang.System\n"); | |
arrayCopyDemo(); | |
System.out.println("Current time in miliseconds " + System.currentTimeMillis()); | |
System.out.println("\n========================"); | |
System.out.println("System.getProperty()"); | |
System.out.println("user.dir: " + System.getProperty("user.dir")); | |
System.out.println("java.home: " + System.getProperty("java.home")); | |
System.out.println("java.specification.version: " + System.getProperty("java.specification.version")); | |
System.out.println("User name: " + System.getProperty("user.name")); | |
System.out.println("\n========================"); | |
System.out.println("System.getSecurityManager()"); | |
SecurityManager s = System.getSecurityManager(); | |
if(s == null) { | |
System.out.println("SecurityManager not been established.."); | |
} else { | |
System.out.println("SecurityManager stablished..."); | |
} | |
hasCodeDemo(); | |
loadLibraryDemo(); | |
finalizationDemo(); | |
propertiesDemo(); | |
System.out.println("\n========================"); | |
System.out.println("System.exit()"); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment