/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tradsecexempel; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Administrator */ public class Main implements Runnable { static int tal = 0; /** * @param args the command line arguments */ public static void main(String[] args) throws InterruptedException { //huvudtråden Thread trad = new Thread(new Main()); trad.start(); int i; int temp; for(i = 0; i < 10; i++) { inkrementera(); System.out.println(tal); } } synchronized static public void inkrementera() throws InterruptedException { int i; int temp; temp = tal; temp++; Thread.sleep(1000); tal = temp; } public void run() { //nya tråden int i; for(i = 0; i < 10; i++) { try { inkrementera(); System.out.println(tal); } catch (InterruptedException ex) { //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } } }