Skip to content

Instantly share code, notes, and snippets.

@ShaneCunn
Created March 29, 2016 13:11
Show Gist options
  • Save ShaneCunn/be26e6f99fb0b1d997a1 to your computer and use it in GitHub Desktop.
Save ShaneCunn/be26e6f99fb0b1d997a1 to your computer and use it in GitHub Desktop.
package Question1;
import java.util.LinkedList;
public class Question1 implements Runnable {
// Linkedlist is faster if your search for data in the beginning or the
// middle of the array
private static final LinkedList<Character> infiList = new LinkedList<>();
//arraylist append to the end of the array, which is normally faster in
// this situation
// static ArrayList<Character> infiList = new ArrayList<Character>();
public static void main(String[] args) {
// creates a new Question object
Question1 threadTester = new Question1();
// creates new Threads and pass in the threadTester object
Thread thread1 = new Thread(threadTester);
Thread thread2 = new Thread(threadTester);
Thread thread3 = new Thread(threadTester);
// starts 3 threads
thread1.start();
thread2.start();
thread3.start();
}
// Synchronized is lock this resource until a thread has finished it, the next thread gain access to the resource
public synchronized void run() {
//creates and initialises variable
// whilst true, then it will keep running infinitely
while (true) {
//creates a Character object which is a wrapper class for primitive type char, this is initialises at letter A
char characters = 'A';
// while is less or equal to Z
while (characters <= 'Z') {
// System.out.println(characters +",");
// add characters to the infiList LinkedList
infiList.add(characters);
// Print out the List , ie get the last element in the array and print it
System.out.println(infiList.get(infiList.size() - 1));
// increments the character counter
characters++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment