Hi
I am trying to do some threading in Android so I took an old threading assignment I had done fora midlet and took out the midlet code and replaced it with android code(such as textview).
package com.assignment1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Threading extends Activity
{
private TextView tortose;
private TextView hare;
private Thread hareThread;
private Thread torotoseThread;
private int num = 0;
private int num2 = 0;
public Threading()
{
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tortose = (TextView) findViewById(R.id.TextView01);
hare = (TextView) findViewById(R.id.TextView02);
Hare newHare = new Hare();
hareThread = new Thread(newHare);
hareThread.start();
Torotose newTortose = new Torotose();
torotoseThread = new Thread(newTortose);
torotoseThread.start();
//updateDisplay();
}
private synchronized void check(int value1, int value2)
{
if((value1-value2) >= 10)
{
try
{
wait();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
private synchronized void getGoing(int value1, int value2)
{
if((value1-value2) == 0)
{
try
{
notify();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
private class Hare extends Thread
{
public void run()
{
while(true)
{
num++;
hare.setText(Integer.toString(num));
check(num, num2);
try
{
// are threads different in andriod apps?
Thread.sleep(100);
// hareThread.sleep(100);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
}
private class Torotose extends Thread
{
public void run()
{
while(true)
{
num2++;
tortose.setText(Integer.toString(num2));
getGoing(num,num2);
try
{
Thread.sleep(200);
//torotoseThread.sleep(200);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
}
}
First it wanted me to change my threads to like static threads.So is this just how Android does it?
Next when I run this code it just crashes with some unexpected error. I am not sure what the error is but when I try to debug it and goes to like to create a new "hare" object it shows me this.
// Compiled from ClassLoader.java (version 1.5 : 49.0, super bit)
public abstract class java.lang.ClassLoader {
// Method descriptor #8 ()V
// Stack: 3, Locals: 1
protected ClassLoader();
0 aload_0 [this]
1 invokespecial java.lang.Object() [1]
4 new java.lang.RuntimeException [2]
7 dup
8 ldc <String "Stub!"> [3]
10 invokespecial java.lang.RuntimeException(java.lang.String) [4]
13 athrow
Line numbers:
[pc: 0, line: 4]
Local variable table:
[pc: 0, pc: 14] local: this index: 0 type: java.lang.ClassLoader
// Method descriptor #14 (Ljava/lang/ClassLoader;)V
// Stack: 3, Locals: 2
protected ClassLoader(java.lang.ClassLoader parentLoader);
0 aload_0 [this]
1 invokespecial java.lang.Object() [1]
4 new java.lang.RuntimeException [2]
7 dup
8 ldc <String "Stub!"> [3]
10 invokespecial java.lang.RuntimeException(java.lang.String) [4]
13 athrow
Line numbers:
[pc: 0, line: 5]
Local variable table:
[pc: 0, pc: 14] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 14] local: parentLoader index: 1 type: java.lang.ClassLoader
// Method descriptor #17 ()Ljava/lang/ClassLoader;
// Stack: 3, Locals: 0
public static java.lang.ClassLoader getSystemClassLoader();
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 6]
// Method descriptor #19 (Ljava/lang/String;)Ljava/net/URL;
// Stack: 3, Locals: 1
public static java.net.URL getSystemResource(java.lang.String resName);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 7]
Local variable table:
[pc: 0, pc: 10] local: resName index: 0 type: java.lang.String
// Method descriptor #23 (Ljava/lang/String;)Ljava/util/Enumeration;
// Signature: (Ljava/lang/String;)Ljava/util/Enumeration<Ljava/net/URL;>;
// Stack: 3, Locals: 1
public static java.util.Enumeration getSystemResources(java.lang.String resName) throws java.io.IOException;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 8]
Local variable table:
[pc: 0, pc: 10] local: resName index: 0 type: java.lang.String
// Method descriptor #29 (Ljava/lang/String;)Ljava/io/InputStream;
// Stack: 3, Locals: 1
public static java.io.InputStream getSystemResourceAsStream(java.lang.String resName);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 9]
Local variable table:
[pc: 0, pc: 10] local: resName index: 0 type: java.lang.String
// Method descriptor #31 ([BII)Ljava/lang/Class;
// Signature: ([BII)Ljava/lang/Class<*>;
// Stack: 3, Locals: 4
protected final java.lang.Class defineClass(byte[] classRep, int offset, int length) throws java.lang.ClassFormatError;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 10]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: classRep index: 1 type: byte[]
[pc: 0, pc: 10] local: offset index: 2 type: int
[pc: 0, pc: 10] local: length index: 3 type: int
// Method descriptor #39 (Ljava/lang/String;[BII)Ljava/lang/Class;
// Signature: (Ljava/lang/String;[BII)Ljava/lang/Class<*>;
// Stack: 3, Locals: 5
protected final java.lang.Class defineClass(java.lang.String className, byte[] classRep, int offset, int length) throws java.lang.ClassFormatError;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 11]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: className index: 1 type: java.lang.String
[pc: 0, pc: 10] local: classRep index: 2 type: byte[]
[pc: 0, pc: 10] local: offset index: 3 type: int
[pc: 0, pc: 10] local: length index: 4 type: int
// Method descriptor #42 (Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
// Signature: (Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class<*>;
// Stack: 3, Locals: 6
protected final java.lang.Class defineClass(java.lang.String className, byte[] classRep, int offset, int length, java.security.ProtectionDomain protectionDomain) throws java.lang.ClassFormatError;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 12]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: className index: 1 type: java.lang.String
[pc: 0, pc: 10] local: classRep index: 2 type: byte[]
[pc: 0, pc: 10] local: offset index: 3 type: int
[pc: 0, pc: 10] local: length index: 4 type: int
[pc: 0, pc: 10] local: protectionDomain index: 5 type: java.security.ProtectionDomain
// Method descriptor #46 (Ljava/lang/String;Ljava/nio/ByteBuffer;Ljava/security/ProtectionDomain;)Ljava/lang/Class;
// Signature: (Ljava/lang/String;Ljava/nio/ByteBuffer;Ljava/security/ProtectionDomain;)Ljava/lang/Class<*>;
// Stack: 3, Locals: 4
protected final java.lang.Class defineClass(java.lang.String name, java.nio.ByteBuffer b, java.security.ProtectionDomain protectionDomain) throws java.lang.ClassFormatError;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 13]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: name index: 1 type: java.lang.String
[pc: 0, pc: 10] local: b index: 2 type: java.nio.ByteBuffer
[pc: 0, pc: 10] local: protectionDomain index: 3 type: java.security.ProtectionDomain
// Method descriptor #52 (Ljava/lang/String;)Ljava/lang/Class;
// Signature: (Ljava/lang/String;)Ljava/lang/Class<*>;
// Stack: 3, Locals: 2
protected java.lang.Class findClass(java.lang.String className) throws java.lang.ClassNotFoundException;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 14]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: className index: 1 type: java.lang.String
// Method descriptor #52 (Ljava/lang/String;)Ljava/lang/Class;
// Signature: (Ljava/lang/String;)Ljava/lang/Class<*>;
// Stack: 3, Locals: 2
protected final java.lang.Class findLoadedClass(java.lang.String className);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 15]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: className index: 1 type: java.lang.String
// Method descriptor #52 (Ljava/lang/String;)Ljava/lang/Class;
// Signature: (Ljava/lang/String;)Ljava/lang/Class<*>;
// Stack: 3, Locals: 2
protected final java.lang.Class findSystemClass(java.lang.String className) throws java.lang.ClassNotFoundException;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 16]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: className index: 1 type: java.lang.String
// Method descriptor #17 ()Ljava/lang/ClassLoader;
// Stack: 3, Locals: 1
public final java.lang.ClassLoader getParent();
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 17]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
// Method descriptor #19 (Ljava/lang/String;)Ljava/net/URL;
// Stack: 3, Locals: 2
public java.net.URL getResource(java.lang.String resName);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 18]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: resName index: 1 type: java.lang.String
// Method descriptor #23 (Ljava/lang/String;)Ljava/util/Enumeration;
// Signature: (Ljava/lang/String;)Ljava/util/Enumeration<Ljava/net/URL;>;
// Stack: 3, Locals: 2
public java.util.Enumeration getResources(java.lang.String resName) throws java.io.IOException;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 19]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: resName index: 1 type: java.lang.String
// Method descriptor #29 (Ljava/lang/String;)Ljava/io/InputStream;
// Stack: 3, Locals: 2
public java.io.InputStream getResourceAsStream(java.lang.String resName);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 20]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: resName index: 1 type: java.lang.String
// Method descriptor #52 (Ljava/lang/String;)Ljava/lang/Class;
// Signature: (Ljava/lang/String;)Ljava/lang/Class<*>;
// Stack: 3, Locals: 2
public java.lang.Class loadClass(java.lang.String className) throws java.lang.ClassNotFoundException;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 21]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: className index: 1 type: java.lang.String
// Method descriptor #62 (Ljava/lang/String;Z)Ljava/lang/Class;
// Signature: (Ljava/lang/String;Z)Ljava/lang/Class<*>;
// Stack: 3, Locals: 3
protected java.lang.Class loadClass(java.lang.String className, boolean resolve) throws java.lang.ClassNotFoundException;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 22]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: className index: 1 type: java.lang.String
[pc: 0, pc: 10] local: resolve index: 2 type: boolean
// Method descriptor #67 (Ljava/lang/Class;)V
// Signature: (Ljava/lang/Class<*>;)V
// Stack: 3, Locals: 2
protected final void resolveClass(java.lang.Class clazz);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 23]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: clazz index: 1 type: java.lang.Class
Local variable type table:
[pc: 0, pc: 10] local: clazz index: 1 type: java.lang.Class<?>
// Method descriptor #19 (Ljava/lang/String;)Ljava/net/URL;
// Stack: 3, Locals: 2
protected java.net.URL findResource(java.lang.String resName);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 24]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: resName index: 1 type: java.lang.String
// Method descriptor #23 (Ljava/lang/String;)Ljava/util/Enumeration;
// Signature: (Ljava/lang/String;)Ljava/util/Enumeration<Ljava/net/URL;>;
// Stack: 3, Locals: 2
protected java.util.Enumeration findResources(java.lang.String resName) throws java.io.IOException;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 25]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: resName index: 1 type: java.lang.String
// Method descriptor #76 (Ljava/lang/String;)Ljava/lang/String;
// Stack: 3, Locals: 2
protected java.lang.String findLibrary(java.lang.String libName);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 26]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: libName index: 1 type: java.lang.String
// Method descriptor #79 (Ljava/lang/String;)Ljava/lang/Package;
// Stack: 3, Locals: 2
protected java.lang.Package getPackage(java.lang.String name);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 27]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: name index: 1 type: java.lang.String
// Method descriptor #81 ()[Ljava/lang/Package;
// Stack: 3, Locals: 1
protected java.lang.Package[] getPackages();
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 28]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
// Method descriptor #83 (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/net/URL;)Ljava/lang/Package;
// Stack: 3, Locals: 9
protected java.lang.Package definePackage(java.lang.String name, java.lang.String specTitle, java.lang.String specVersion, java.lang.String specVendor, java.lang.String implTitle, java.lang.String implVersion, java.lang.String implVendor, java.net.URL sealBase) throws java.lang.IllegalArgumentException;
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 29]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: name index: 1 type: java.lang.String
[pc: 0, pc: 10] local: specTitle index: 2 type: java.lang.String
[pc: 0, pc: 10] local: specVersion index: 3 type: java.lang.String
[pc: 0, pc: 10] local: specVendor index: 4 type: java.lang.String
[pc: 0, pc: 10] local: implTitle index: 5 type: java.lang.String
[pc: 0, pc: 10] local: implVersion index: 6 type: java.lang.String
[pc: 0, pc: 10] local: implVendor index: 7 type: java.lang.String
[pc: 0, pc: 10] local: sealBase index: 8 type: java.net.URL
// Method descriptor #94 (Ljava/lang/Class;[Ljava/lang/Object;)V
// Signature: (Ljava/lang/Class<*>;[Ljava/lang/Object;)V
// Stack: 3, Locals: 3
protected final void setSigners(java.lang.Class c, java.lang.Object[] signers);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 30]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: c index: 1 type: java.lang.Class
[pc: 0, pc: 10] local: signers index: 2 type: java.lang.Object[]
Local variable type table:
[pc: 0, pc: 10] local: c index: 1 type: java.lang.Class<?>
// Method descriptor #100 (Ljava/lang/String;Z)V
// Stack: 3, Locals: 3
public void setClassAssertionStatus(java.lang.String cname, boolean enable);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 31]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: cname index: 1 type: java.lang.String
[pc: 0, pc: 10] local: enable index: 2 type: boolean
// Method descriptor #100 (Ljava/lang/String;Z)V
// Stack: 3, Locals: 3
public void setPackageAssertionStatus(java.lang.String pname, boolean enable);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 32]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: pname index: 1 type: java.lang.String
[pc: 0, pc: 10] local: enable index: 2 type: boolean
// Method descriptor #106 (Z)V
// Stack: 3, Locals: 2
public void setDefaultAssertionStatus(boolean enable);
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 33]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
[pc: 0, pc: 10] local: enable index: 1 type: boolean
// Method descriptor #8 ()V
// Stack: 3, Locals: 1
public void clearAssertionStatus();
0 new java.lang.RuntimeException [2]
3 dup
4 ldc <String "Stub!"> [3]
6 invokespecial java.lang.RuntimeException(java.lang.String) [4]
9 athrow
Line numbers:
[pc: 0, line: 34]
Local variable table:
[pc: 0, pc: 10] local: this index: 0 type: java.lang.ClassLoader
}
So I am not sure where I went wrong.
Thanks