-
-
Save Foredoomed/5011115 to your computer and use it in GitHub Desktop.
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
public class LockTest { | |
private static final String fileName = "C:/work/LockExample.txt"; | |
private static final String EXIT_FLAG = "BYE"; | |
private static final int NO_OF_LINES = 10; | |
private static final Lock fileLock = new ReentrantLock(); | |
private static final Condition condition = fileLock.newCondition(); | |
private static final ExecutorService executorPool = Executors.newFixedThreadPool(2); | |
public static void main(String[] args) { | |
Runnable writer = new FileWrite(); | |
Runnable reader = new FileRead(); | |
executorPool.submit(writer); | |
executorPool.submit(reader); | |
executorPool.shutdown(); | |
} | |
public static class FileWrite implements Runnable { | |
public void run() { | |
try { | |
fileLock.lock(); | |
for (int i = 0; i < NO_OF_LINES; i++) { | |
PrintWriter writer = new PrintWriter(new File(fileName)); | |
if (i != NO_OF_LINES - 1) { | |
int random = new SecureRandom().nextInt(); | |
System.out.println("Writer writing..." + random); | |
writer.println(random); | |
writer.close(); | |
condition.signal(); | |
System.out.println("Writer waiting"); | |
condition.await(); | |
} else { | |
System.out.println(EXIT_FLAG); | |
System.out.println("Writer exiting"); | |
writer.close(); | |
condition.signal(); | |
} | |
} | |
} catch (Exception e) { | |
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!EXCEPTION!!!!!!!!!!!!!!!!!!!!!!!!"); | |
e.printStackTrace(); | |
} finally { | |
fileLock.unlock(); | |
// Delete the file, require in case if one wants to run demo | |
// again. | |
File file = new File(fileName); | |
file.delete(); | |
try { | |
file.createNewFile(); | |
} catch (Exception e) { | |
} | |
} | |
} | |
} | |
public static class FileRead implements Runnable { | |
public void run() { | |
String data = null; | |
fileLock.lock(); | |
try { | |
while (true) { | |
BufferedReader reader = new BufferedReader(new FileReader(fileName)); | |
data = reader.readLine(); | |
System.out.println("READ DATA - " + data); | |
reader.close(); | |
if (data == null || !data.equals(EXIT_FLAG)) { | |
condition.signalAll(); | |
System.out.println("Reader Waiting"); | |
condition.await(); | |
} else { | |
System.out.println("READER EXITING"); | |
condition.signal(); | |
break; | |
} | |
} | |
} catch (Exception e) { | |
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!EXCEPTION!!!!!!!!!!!!!!!!!!!!!!!!"); | |
e.printStackTrace(); | |
} finally { | |
fileLock.unlock(); | |
} | |
} | |
} | |
} | |
==================================================================================================================== | |
public class ListTest { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
List<Integer> list = new ArrayList<Integer>(); | |
list.add(1); | |
list.add(2); | |
list.add(3); | |
list.add(4); | |
ListIterator fwd = list.listIterator(); | |
ListIterator rev = list.listIterator(list.size()); | |
for (int i = 0, mid = list.size() >> 1; i < mid; i++) { | |
Object tmp = fwd.next(); | |
fwd.set(rev.previous()); | |
rev.set(tmp); | |
} | |
for (int a : list) { | |
System.out.println(a); | |
} | |
} | |
} | |
=============================================================================================== | |
public class Test10 { | |
private static Test10 sigleton=new Test10(); | |
private static int count1; | |
private static int count2=0; | |
private Test10(){ | |
count1++; | |
count2++; | |
} | |
public static Test10 getInstance(){ | |
return sigleton; | |
} | |
public static void main(String[] args) { | |
Test10 t= new Test10(); | |
System.out.println(t.count1); | |
System.out.println(t.count2); | |
} | |
} | |
=============================================================== | |
public class Queens { | |
static int q = 10; | |
static int[] i = new int[q]; | |
static int count = 0; | |
public static void main(String[] args) { | |
long t = System.currentTimeMillis(); | |
scan(0); | |
System.out.println("total time: " + (System.currentTimeMillis() - t)); | |
System.out.println("total results: " + count); | |
} | |
private static void scan(int n) { | |
if (n == q) { | |
// for (int k=0;k<q;k++) System.out.print(i[k]+(k==q-1?"\n":",")); | |
count++; | |
return; | |
} | |
i[n] = 0; | |
while (i[n] < q) { | |
i[n] = i[n] + 1; | |
if (check(n)) | |
scan(n + 1); | |
} | |
} | |
private static boolean check(int n) { | |
for (int j = 0; j < n; ++j) | |
if (i[j] == i[n] || i[j] - i[n] == j - n || i[j] - i[n] == n - j) | |
return false; | |
return true; | |
} | |
} | |
=================================================================== | |
public class SingletionTest { | |
private SingletionTest(){} | |
private static class lazyholder{ | |
private static final SingletionTest instance = new SingletionTest(); | |
} | |
public static SingletionTest getInstance(){ | |
return lazyholder.instance; | |
} | |
} | |
============================================================================ | |
public class Test3 { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
for (int i = 0; i < 10; i++) { | |
System.out.println(Test3.class.getName()); | |
} | |
} | |
void addTest(List<? super Dog> list) { | |
list.add(new Dog()); | |
//list.add(new Animal());//Line1 | |
//list.add(new Object());//Line2 | |
list.add(new a()); | |
//list.add(new b()); | |
} | |
class Dog extends b{ | |
} | |
class Animal extends Dog { | |
} | |
class a extends Dog{ | |
} | |
class b{ | |
} | |
} | |
================================================================ | |
public class Quicksort1 { | |
public static void main(String[] args) { | |
int[] array = { 2, 43, 65, 78, 9, 0, 3, 3, 6, 7, 4, 2, 4, 77 }; | |
for (int number : array) { | |
System.out.print(number + " "); | |
} | |
System.out.println(); | |
quickSort(array, 0, array.length - 1); | |
for (int number : array) { | |
System.out.print(number + " "); | |
} | |
System.out.println(); | |
} | |
public static void quickSort(int[] array, int low, int high) { | |
if (low > high) { | |
return; | |
} | |
// int k = low + new Random().nextInt(high - low + 1); | |
// swap(array, low, k); | |
int pivot = array[low]; | |
int j = low; | |
for (int i = low + 1; i <= high; i++) { | |
if (array[i] < pivot) { | |
swap(array, ++j, i); | |
} | |
} | |
swap(array, low, j); | |
quickSort(array, low, j - 1); | |
quickSort(array, j + 1, high); | |
} | |
public static void swap(int[] array, int i, int j) { | |
int temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
} | |
} | |
public class Quicksort2 { | |
static int[] a = { 2, 43, 65, 78, 9, 0, 3, 3, 6, 7, 4, 2, 4, 77 }; | |
private int[] numbers; | |
private int number; | |
public void sort(int[] values) { | |
// Check for empty or null array | |
if (values == null || values.length == 0) { | |
return; | |
} | |
this.numbers = values; | |
number = values.length; | |
quicksort(0, number - 1); | |
} | |
private void quicksort(int low, int high) { | |
int i = low, j = high; | |
// Get the pivot element from the middle of the list | |
int pivot = numbers[low + (high - low) / 2]; | |
// Divide into two lists | |
while (i <= j) { | |
// If the current value from the left list is smaller then the pivot | |
// element then get the next element from the left list | |
while (numbers[i] < pivot) { | |
i++; | |
} | |
// If the current value from the right list is larger then the pivot | |
// element then get the next element from the right list | |
while (numbers[j] > pivot) { | |
j--; | |
} | |
// If we have found a values in the left list which is larger then | |
// the pivot element and if we have found a value in the right list | |
// which is smaller then the pivot element then we exchange the | |
// values. | |
// As we are done we can increase i and j | |
if (i <= j) { | |
exchange(i, j); | |
i++; | |
j--; | |
} | |
} | |
// Recursion | |
if (low < j) | |
quicksort(low, j); | |
if (i < high) | |
quicksort(i, high); | |
} | |
private void exchange(int i, int j) { | |
int temp = numbers[i]; | |
numbers[i] = numbers[j]; | |
numbers[j] = temp; | |
} | |
private void ab(int[] a, int low, int high){ | |
int i = low; | |
int j = high; | |
int pivot = a[low + (high-low)/2]; | |
while(i<=j){ | |
while(a[i]<pivot) | |
i++; | |
while(a[j]>pivot) | |
j--; | |
if(i<=j){ | |
exchange(i,j); | |
i++; | |
j--; | |
} | |
if(i < high) | |
ab(a,i,high); | |
if(j > low) | |
ab(a,low, j); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment