Multithreading and syncing arrays

wodetzky

New Member
Hi I am running multiple threads in my program with each thread contaning an array of 6 elements. I have added synchronize in my run method for the thread but only the first thread is synchronised in terms of displaying the array orderly from 1 to 6.\[code\]/* My run method for the thread */public synchronized void run(){ int from = St7.stt[acnt].getCf(); int to = St7.stt[acnt].getCt(); int inc = St7.stt[acnt].getCi(); threadsRunning++; // We now have more threads this.threadId = threadsRunning; active = true; try{ int loop = from; while (active && loop <= to){ System.out.println(text + " Counter: " + St7.stt[acnt].getCounter() + "."); System.out.println(); Thread.sleep(sleep); loop=loop+inc; }\[/code\]And this is my main.\[code\]public class St7 {public static Student[] stt = new Student[6];public static void multipleThreads(){ for(int x = 0; x<6; x++){ StudentThread stthread = new StudentThread("ID: " + stt[x].getId()+" - " + stt[x].getType() + ": " + stt[x].getName()+ ", Count from: " + stt[x].getCf()+", Count to: " + stt[x].getCt()+", Count by: " + stt[x].getCi()+",",x,1000,6); stthread.start(); }}public static void main(String[] args) { Connection con = null; String url = "jdbc:mysql://****"; // The connection string String db = "****"; // Which Database? String driver = "com.mysql.jdbc.Driver"; // Always the same for MySQL String user = "***"; // Username for authorisation String pass = "****"; // Password try { /* Get the MySQL driver loaded. This is done at runtime */ Class.forName(driver).newInstance(); /* Get the connection from the DriverManager */ con = DriverManager.getConnection(url + db, user, pass); try { Statement st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM students"); //System.out.println("Id\tName\t\tCourse"); int var = 0; while (rs.next()) { //int id = rs.getInt("id"); //String name = rs.getString("name"); //String course = rs.getString("course"); //System.out.println(id + "\t" + name + "\t" + course); if(var==0){ stt[var] = new MathStudent(); stt[var].setId(rs.getInt("id")); stt[var].setName(rs.getString("name")); stt[var].setCf(rs.getInt("counter_from")); stt[var].setCt(rs.getInt("counter_to")); stt[var].setCi(rs.getInt("counter_increment")); stt[var].setCount(rs.getInt("counter_from")); }else if (var==1){ stt[var] = new ComputerStudent(); stt[var].setId(rs.getInt("id")); stt[var].setName(rs.getString("name")); stt[var].setCf(rs.getInt("counter_from")); stt[var].setCt(rs.getInt("counter_to")); stt[var].setCi(rs.getInt("counter_increment")); stt[var].setCount(rs.getInt("counter_from")); }else if (var==2){ stt[var] = new ScienceStudent(); stt[var].setId(rs.getInt("id")); stt[var].setName(rs.getString("name")); stt[var].setCf(rs.getInt("counter_from")); stt[var].setCt(rs.getInt("counter_to")); stt[var].setCi(rs.getInt("counter_increment")); stt[var].setCount(rs.getInt("counter_from")); }else if (var==3){ stt[var] = new ComputerStudent(); stt[var].setId(rs.getInt("id")); stt[var].setName(rs.getString("name")); stt[var].setCf(rs.getInt("counter_from")); stt[var].setCt(rs.getInt("counter_to")); stt[var].setCi(rs.getInt("counter_increment")); stt[var].setCount(rs.getInt("counter_from")); }else if (var==4){ stt[var] = new MathStudent(); stt[var].setId(rs.getInt("id")); stt[var].setName(rs.getString("name")); stt[var].setCf(rs.getInt("counter_from")); stt[var].setCt(rs.getInt("counter_to")); stt[var].setCi(rs.getInt("counter_increment")); stt[var].setCount(rs.getInt("counter_from")); }else{ stt[var] = new ScienceStudent(); stt[var].setId(rs.getInt("id")); stt[var].setName(rs.getString("name")); stt[var].setCf(rs.getInt("counter_from")); stt[var].setCt(rs.getInt("counter_to")); stt[var].setCi(rs.getInt("counter_increment")); stt[var].setCount(rs.getInt("counter_from")); } var++; } multipleThreads();\[/code\]My Output is this, after id 6, the next thread starts but id is not following order from 1-6.\[code\]ID: 1ID: 2ID: 3ID: 4ID: 5ID: 6ID: 3ID: 5ID: 1...\[/code\]I want this end result:\[code\]ID: 1...ID: 2...ID: 3...ID: 4...ID: 5...ID: 6...ID: 1...ID: 2...ID: 3...ID: 4...ID: 5...ID: 6...ID: 1...ID: 2...ID: 3...ID: 4...ID: 5...ID: 6...\[/code\]
 
Back
Top