Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Thread problem.

jeffy_khorjeffy_khor Member Posts: 163
I try to do thread but fail. Can anybody check what wrong with this code.

[code]
class GoAgain implements Runnable{ //extends Thread{
Thread c;
int i;

public GoAgain(String name) {
Thread c = new Thread(this,name);
i = 0;
c.start();
}

public void run(){
while(i<5){
i++;
System.out.println(i);
try{
c.sleep(500);
}catch(InterruptedException e){
System.out.println("It has been interrupted");
}
}
}
}

public class example2 {

public example2() {
}

public static void main(String[] args) {
GoAgain t = new GoAgain("Hello");
try{
t.c.join();
}catch(InterruptedException e){
System.out.println("It has been interrupted.");
}
System.out.println("Finish");
}
}


[/code]

Comments

  • NIN666NIN666 Member Posts: 90
    : I try to do thread but fail. Can anybody check what wrong with this code.
    :
    : [code]
    : class GoAgain implements Runnable{ //extends Thread{
    : Thread c;
    : int i;
    :
    : public GoAgain(String name) {
    : Thread c = new Thread(this,name);
    : i = 0;
    : c.start();
    : }
    :
    : public void run(){
    : while(i<5){
    : i++;
    : System.out.println(i);
    : try{
    : c.sleep(500);
    : }catch(InterruptedException e){
    : System.out.println("It has been interrupted");
    : }
    : }
    : }
    : }
    :
    : public class example2 {
    :
    : public example2() {
    : }
    :
    : public static void main(String[] args) {
    : GoAgain t = new GoAgain("Hello");
    : try{
    : t.c.join();
    : }catch(InterruptedException e){
    : System.out.println("It has been interrupted.");
    : }
    : System.out.println("Finish");
    : }
    : }
    :
    :
    : [/code]
    :

    I have a question, what language is this?
  • jeffy_khorjeffy_khor Member Posts: 163
    : : I try to do thread but fail. Can anybody check what wrong with this code.
    : :
    : : [code]
    : : class GoAgain implements Runnable{ //extends Thread{
    : : Thread c;
    : : int i;
    : :
    : : public GoAgain(String name) {
    : : Thread c = new Thread(this,name);
    : : i = 0;
    : : c.start();
    : : }
    : :
    : : public void run(){
    : : while(i<5){
    : : i++;
    : : System.out.println(i);
    : : try{
    : : c.sleep(500);
    : : }catch(InterruptedException e){
    : : System.out.println("It has been interrupted");
    : : }
    : : }
    : : }
    : : }
    : :
    : : public class example2 {
    : :
    : : public example2() {
    : : }
    : :
    : : public static void main(String[] args) {
    : : GoAgain t = new GoAgain("Hello");
    : : try{
    : : t.c.join();
    : : }catch(InterruptedException e){
    : : System.out.println("It has been interrupted.");
    : : }
    : : System.out.println("Finish");
    : : }
    : : }
    : :
    : :
    : : [/code]
    : :
    :
    : I have a question, what language is this?
    :

    Ar??? What wrong.

Sign In or Register to comment.