0% found this document useful (0 votes)
12 views21 pages

Thread in Adv Java

The document describes creating two threads - a producer thread and a consumer thread that share access to a common Company object. The producer thread calls the Company object's produce_item method to generate items, while the consumer thread calls the consume_item method to retrieve items. Synchronized methods are used to ensure thread-safe access to the shared Company object.

Uploaded by

tusharsharma2029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views21 pages

Thread in Adv Java

The document describes creating two threads - a producer thread and a consumer thread that share access to a common Company object. The producer thread calls the Company object's produce_item method to generate items, while the consumer thread calls the consume_item method to retrieve items. Synchronized methods are used to ensure thread-safe access to the shared Company object.

Uploaded by

tusharsharma2029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

//creating our thread using Runnable interface

class MyThread implements Runnable


{
public void run(){
// task for thread...
for(int i=1;i<=10;i++)
{
System.out.println("value of i is "+i);
}
}
public static void main(String[] args){
//create object of Mythread class
MyThread t1=new MyThread();
t1.start();
}
}
result:-
MyThread.java:14: error: cannot find symbol
t1.start();
^
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++)
{
System.out.println("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);
thr.start();
}
}

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++)
{
System.out.println("value of i is "+i);
try {
Thread.sleep(5000);
}catch(Exception e){
}
}
}
public static void main(String[] args){
//create object of Mythread class
MyThread t1=new MyThread();
Thread thr=new Thread(t1);
thr.start();
}
}

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
• System.out.println("value of i is "+i);
• try { • for(int i=10;i>=1;i--)
• Thread.sleep(5000); • {
• }catch(Exception e){ • System.out.println("another thread= "+i);
• }
• }
• try
• } • {
• public static void main(String[] args){ • Thread.sleep(2000);


//create object of Mythread class
MyThread t1=new MyThread();
• }catch(Exception e) Result :-
• Thread thr=new Thread(t1); • { Javac MyAnotherThread.java
• //object of anotherthread • }
• MyAnotherThread t2=new MyAnotherThread();
Cls
• thr.start();
• } Javac MyAnotherThread.java
• t2.start();
• } • } Cls
• } • } Javac MyThread.java
Java MyThread
Some methods
1st (getname,sleep,rename)
class ThreadOp{
public static void main(String[] args) {
System.out.println("program started...");
int x=56+34;
System.out.println("sum is "+x);
//thread
Thread t=Thread.currentThread();
String tname=t.getName();
System.out.println("current running thread is "+tname);
//setName
t.setName("MyMain");
System.out.println(t.getName());
try{
Thread.sleep(5000);
}catch(Exception e){};
System.out.println("program ended...");
}
}
• class ThreadOp
Getid();
• {
• public static void main(String[] args) {
• System.out.println("program started...");
• int x=56+34;
• System.out.println("sum is "+x);
• //thread
• Thread t=Thread.currentThread();
• String tname=t.getName();
• System.out.println("current running thread is "+tname);

• //setName
• t.setName("MyMain");
• System.out.println(t.getName());
• try{
• Thread.sleep(5000);
• }catch(Exception e){
• };
• System.out.println(t.getId());

• System.out.println("program ended...");
• }
• }
1st company
• class Company
• {
• int n;
• synchronized public void produce_item(int n)
• {
• this.n=n;
• System.out.println("Produced : "+this.n);
• }

synchronized public int consume_item()
• {
• System.out.println("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{
• Thread.sleep(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{Thread.sleep(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);
• p.start();
• c.start();
• }
• }
Run in cmd
• Javac Company.java
• Javac Producer.java
• Javac Consumer.java
• Javac Main.java
• Java Main

You might also like