Stanislav Zorjan - Stasha - Full Stack Software Engineer and Game Development Hobbyist - Prague


1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
      a) Simple User entity class

 

 

package basics.basicsExamples;


/** ****************************************************************************
 * http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html
   http://www.tutorialspoint.com/java/java_multithreading.htm
 * 
 * MULTITHREADING EXAMPLE CLASS
 * 
 * TODO Synchronization, deadlocking   
 * 
 * @author stanislav.zorjan
 */
public class MultiThreadingExample implements Runnable {
    
    
    public static int staticCounter = 0;
    private static int synCounter = 0;
    
    private int localCounter = 0;
    

    /** ************************************************************************
     * Default constructor
     */
    public MultiThreadingExample() {
	AnimatingThread ttc = new AnimatingThread("Thread 0");
	AnimatingThread ttc2 = new AnimatingThread("Thread 1");
	
	ttc.start();
	ttc2.start();
	
	run();
    }

    @Override
    public final void run() { 
	try {
	    for(int i = 0; i < 10; ++i){
		
		if (Thread.interrupted()) {
		    throw new InterruptedException();
		}
		
		System.out.println("Internal Thread static: "+MultiThreadingExample.staticCounter++);
		System.out.println("Internal Thread local: "+localCounter++);
		System.out.println("");
	    
		Thread.sleep(100);
	    }
	} catch (InterruptedException ex) {
	    ex.printStackTrace();
	}finally{
	    System.out.println("cleaning up");
	}
    }
}



/** ****************************************************************************
 * SIMPLE THREAD TEST CLASS
 * @author stanislav.zorjan
 */
class AnimatingThread extends Thread{
    
    public int counter = 0;
    private String name;
    
    
    public AnimatingThread(String name){
	this.name = name;
    }
    
    @Override
    public void run(){ 
	try {
	    for(int i = 0; i < 10; ++i){
		
		if (Thread.interrupted()) {
		    throw new InterruptedException();
		}
		
		System.out.println(this.name+" static: "+MultiThreadingExample.staticCounter++);
		System.out.println(this.name+" local: "+counter++);
		System.out.println("");


		Thread.sleep(100);
	    }
	} catch (InterruptedException ex) {
	    ex.printStackTrace();
	}finally{
	    System.out.println("cleaning up");
	}
    }    
}