//creating our thread using Runnable interface
class MyThread implements Runnable
{
public void run(){
// task for thread...
for(int i=1;i<=10;i++)
{
[Link]("value of i is "+i);
}
}
public static void main(String[] args){
//create object of Mythread class
MyThread t1=new MyThread();
[Link]();
}
}
result:-
[Link][Link] error: cannot find symbol
[Link]();
^
symbol: method start()
location: variable t1 of type MyThread
1 error
//creating our thread using Runnable interface
class MyThread implements Runnable
{
public void run(){
// task for thread...
for(int i=1;i<=10;i++)
{
[Link]("value of i is "+i);
}
}
public static void main(String[] args){
//create object of Mythread class
MyThread t1=new MyThread();
Thread thr=new Thread(t1);
[Link]();
}
}
Result:
value of i is 1
value of i is 2
value of i is 3
value of i is 4
value of i is 5
value of i is 6
value of i is 7
value of i is 8
value of i is 9
value of i is 10
//creating our thread using Runnable interface
class MyThread implements Runnable
{
public void run(){
// task for thread...
for(int i=1;i<=10;i++)
{
[Link]("value of i is "+i);
try {
[Link](5000);
}catch(Exception e){
}
}
}
public static void main(String[] args){
//create object of Mythread class
MyThread t1=new MyThread();
Thread thr=new Thread(t1);
[Link]();
}
}
Result slow show
2 file create
• 1st file • 2nd file
• //creating our thread using Runnable interface • //create thread using thread class
• class MyThread implements Runnable • class MyAnotherThread extends Thread
• {
•
• {
public void run(){
• // task for thread... • public void run()
• for(int i=1;i<=10;i++) • {
• {
• //task for thread
• [Link]("value of i is "+i);
• try { • for(int i=10;i>=1;i--)
• [Link](5000); • {
• }catch(Exception e){ • [Link]("another thread= "+i);
• }
• }
• try
• } • {
• public static void main(String[] args){ • [Link](2000);
•
•
//create object of Mythread class
MyThread t1=new MyThread();
• }catch(Exception e) Result :-
• Thread thr=new Thread(t1); • { Javac [Link]
• //object of anotherthread • }
• MyAnotherThread t2=new MyAnotherThread();
Cls
• [Link]();
• } Javac [Link]
• [Link]();
• } • } Cls
• } • } Javac [Link]
Java MyThread
Some methods
1st (getname,sleep,rename)
class ThreadOp{
public static void main(String[] args) {
[Link]("program started...");
int x=56+34;
[Link]("sum is "+x);
//thread
Thread t=[Link]();
String tname=[Link]();
[Link]("current running thread is "+tname);
//setName
[Link]("MyMain");
[Link]([Link]());
try{
[Link](5000);
}catch(Exception e){};
[Link]("program ended...");
}
}
• class ThreadOp
Getid();
• {
• public static void main(String[] args) {
• [Link]("program started...");
• int x=56+34;
• [Link]("sum is "+x);
• //thread
• Thread t=[Link]();
• String tname=[Link]();
• [Link]("current running thread is "+tname);
• //setName
• [Link]("MyMain");
• [Link]([Link]());
• try{
• [Link](5000);
• }catch(Exception e){
• };
• [Link]([Link]());
• [Link]("program ended...");
• }
• }
1st company
• class Company
• {
• int n;
• synchronized public void produce_item(int n)
• {
• this.n=n;
• [Link]("Produced : "+this.n);
• }
•
synchronized public int consume_item()
• {
• [Link]("Consumed : "+this.n);
• return this.n;
• }
•
}
2 Producer
nd
• class Producer extends Thread
• {
• Company c;
• Producer(Company c)
• {
• this.c=c;
• }
• public void run()
• {
• int i=1;
• while(true)
• {
• this.c.produce_item(i);
• try{
• [Link](1000);}catch(Exception e){}
• i++;
• }
• }
• }
3 Consumer
rd
• class Consumer extends Thread
• {
• Company c;
• Consumer(Company c)
• {
• this.c=c;
• }
• public void run()
• {
• while(true)
• {
• this.c.consume_item();
• try{[Link](2000);}catch(Exception e){}
• }
• }
• }
4 Main
th
• class Main
• {
• public static void main(String[] args) {
• Company comp=new Company();
• Producer p=new Producer(comp);
• Consumer c=new Consumer(comp);
• [Link]();
• [Link]();
• }
• }
Run in cmd
• Javac [Link]
• Javac [Link]
• Javac [Link]
• Javac [Link]
• Java Main