A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Threads and Synchronization
Author
Sharing variable between two threads.
Joseph Cho
Ranch Hand
Joined: Feb 19, 2012
Posts: 36
posted
Nov 09, 2012 00:14:36
0
I'm trying to have one thread keep track of the time while the main thread does the work.. For some reason the thread is not getting the updated variable, how do I get this to work? I tried using a static variable but it's not working..
Here is the code:
package QuestionProgram; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import javax.swing.JPanel; public class QuestionPanel extends JPanel implements Runnable { private TimerPanel timerPanel; private AnswerPanel answerPanel; private Thread thread; public ArrayList<Question> questions = new ArrayList<Question>(); int arrayIndex = 0; static boolean timeIsUp = false; static boolean testFinished = false; boolean firstTime = true; boolean firstTimeFill = true; public QuestionPanel() { this.setBackground(Color.green); } public void go() { thread = new Thread(this); thread.start(); if(firstTimeFill) { fileToArray(); } firstTime = false; repaint(); } void setObjects(AnswerPanel answerPanel, TimerPanel timerPanel) { this.answerPanel = answerPanel; this.timerPanel = timerPanel; } public void run() { System.out.println("inside run firstTimeFill= " + firstTimeFill); while ((!timeIsUp) && (!testFinished)) { // timeIsUp = this.timeIsUp; // testFinished = this.testFinished; System.out.println("enters thread, testFinsihed=" + testFinished); System.out.println("enters thread, timeIsUp=" + timeIsUp); try { // Thread.sleep(1000); Thread.sleep(1000); }catch(InterruptedException e) { e.printStackTrace();} //currentTime=System.currentTimeMillis(); } timerPanel.stopTimer(); System.out.println("test is over"); } void fileToArray() { // Location of file to read firstTimeFill = false; File file = new File("quiz.txt"); String line = "test"; String answer = "test"; try { Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { line = scanner.nextLine(); //get line line = line.trim(); String check = line.substring(line.length() - 3); //store last three characters to check for jpg check = check.trim(); //trim any whitespace if(scanner.hasNextLine()) { answer = scanner.nextLine(); } if(check.equals("jpg")) { questions.add(new ImageQuestion(line, answer, this)); } else { questions.add(new TextQuestion(line, answer, this)); } }//end while scanner.close(); }//end try catch (FileNotFoundException e) { e.printStackTrace(); }//end catch } public String getTotalPts() { int total = 0; for(int i=0; i<questions.size(); i++) { total = total + questions.get(i).score; } return Integer.toString(total); } @SuppressWarnings("deprecation") void reset() { arrayIndex = 0; answerPanel.counter=0; firstTime = true; timeIsUp = false; testFinished = false; //thread.stop(); } public void paintComponent(Graphics g) { super.paintComponent(g); if(firstTime) return; System.out.println(arrayIndex); System.out.println(questions.size()); if(arrayIndex < questions.size()) { Dimension d = new Dimension(); d.height = 0; d.width = 0; System.out.println(arrayIndex); questions.get(arrayIndex).draw(g, d); } else { testFinished = true; timerPanel.stopTimer(); g.drawString("Test Complete!", 12, 20); g.drawString("Your Results are:", 12, 40); g.drawString(getTotalPts(), 12, 60); reset(); } } }
Rakesh K. Cherukuri
Ranch Hand
Joined: Jun 01, 2010
Posts: 47
I like...
posted
Nov 09, 2012 04:40:41
0
Duplicate of
http://www.coderanch.com/t/597439/threads/java/Sharing-variable-between-two-threads
The other one already has responses
Warm Regards,
Rakesh
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: Sharing variable between two threads.
Similar Threads
how to split a text file
Code Optimizatoin
File Operations Program Problem
Parsing a text file to an arraylist
Reading the last (or last but 1) line in a text file
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter