• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Question

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which code insert here to make j = 10;
public class test1 implements Runnable
{
public static int j = 0;
public void run()
{
try {
Thread.sleep(5000);
j = 10;
} catch (InterruptException e)
{
j = 8;
}
}

public static void main(String arg[])
{
Thread t = new Thread(this);
t.start();
//insert here
System.out.println(""+j);
}
}

which code insert here to make j = 10;
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code has a few problems. First it must catch InterruptedExceptions and not InterruptExceptions and second you can't have this in a static method. So you are getting two compile errors.
Now, get rid of those errors and it will print 0, pause for 5 seconds, and then exit out of main. Reason is becuase you start a new Thread, pause for 5 seconds and then change the value of j. But you main programs thread access the value of j before you changed j, and since j is not synchronized, you are getting the outout of 0 then exit.
How about where it says insert here saying:
j=10;
Bill
[This message has been edited by bill bozeman (edited January 08, 2001).]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Here I inserted some code to make j=10.
But it only runs correctly when that else statement is included.
Otherwise, program keeps running but no output. Why?
--HU
public class test1 implements Runnable
{
public static int j = 0;
public void run()
{
try {
Thread.sleep(5000);
j=10;
} catch (InterruptedException e)
{
j = 8;
}
}
public static void main(String arg[])
{
test1 t1 = new test1();
Thread t = new Thread(t1);
t.start();
//insert here
while(true){
if(j==10)break;
else System.out.print("");
}
System.out.println(" "+j);
}
}
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class test1 implements Runnable
{
public static int j = 0;
public void run()
{
try {
Thread.sleep(5000);
j = 10;
} catch (InterruptedException e)
{
j = 8;
}
}
public static void main(String arg[])
{
test1 test=new test1();
Thread t = new Thread(test);
t.start();
try{t.join();}catch(InterruptedException e ){}
System.out.println(""+j);
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic