I'm just studying for an exam and I have the following non thread-safe code from a previous exam question:
class Q3 { private boolean f[] = new boolean[20]; public Q3() { for (int j = 0; j < 20; j++) { f[j] = true; } } public void negate(int a, int b) { for(int j = a; j < b; j++) { f[j] = !f[j]; } } public void swap(int a, int b) { boolean temp = f[a]; f[a] = f[b]; f[b] = temp; } } I made it thread safe by making the following adjustments:
class Q3 { private boolean f[] = new boolean[20]; public Q3() { for (int j = 0; j < 20; j++) { f[j] = true; } } synchronized void negate(int a, int b) { for (int j = a; j < b; j++) { f[j] = !f[j]; } } synchronized void swap(int a, int b) { boolean temp = f[a]; f[a] = f[b]; f[b] = temp; } } But could somebody explain to me why the original code is not thread safe? I know how to make code thread safe once it isn't so, but i'm still unsure as to why code is/is not defined as being thread safe
Q3class is being used?android:minSdkVersion="10"should be<uses-sdk android:minSdkVersion="10" />. Your build must be failing