0% found this document useful (0 votes)
101 views

Object Oriented Programming Methodology Using Java: Prof: Pradnya Sadigale (E&Tcdepartment)

This document discusses object oriented programming using Java threads. It defines a thread as a single sequence of execution within a program. Java threads allow programs to perform multiple tasks simultaneously. There are two ways to create threads in Java: by extending the Thread class or implementing the Runnable interface. Examples are provided to demonstrate creating threads using both approaches and printing output sequentially using the sleep() method. The document also outlines the typical life cycle of a thread from starting to joining with another thread.

Uploaded by

pradnya sadigale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Object Oriented Programming Methodology Using Java: Prof: Pradnya Sadigale (E&Tcdepartment)

This document discusses object oriented programming using Java threads. It defines a thread as a single sequence of execution within a program. Java threads allow programs to perform multiple tasks simultaneously. There are two ways to create threads in Java: by extending the Thread class or implementing the Runnable interface. Examples are provided to demonstrate creating threads using both approaches and printing output sequentially using the sleep() method. The document also outlines the typical life cycle of a thread from starting to joining with another thread.

Uploaded by

pradnya sadigale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Object Oriented Programming Methodology

Using Java

Prof: Pradnya Sadigale


(E&TCDepartment)
SOET-Lohegaon
•Set of statements or small code or a task
•Threads are required when tasks are to be executed simultaneously
A thread is a single sequence of execution within a program
Java threads allow you to write programs that do many
things at once. Each thread represents an independently
executing sequence of control
Write a program to display current therad .
import java.io.*;
class Main
{ public static void main(String args[])
{
Thread t1=Thread.currentThread();
System.out.println(t1);
}}
Thead[main,5,main]
Type -1:Extend Thread
Class Mythread extends Thread
{
String Myname;
Mythraed ()
{ Myname =“Mythread”;
}
void run()
{ Scanner s=new Scanner(System.in);
SOP(“enter name”);
name=s.next();
} }
Class theraddemo
{
PSVM(String args[])
{ Thread t=new Mythread();
t.start();
}}
Type 2:IMPLEMENT RUNNABLE Thread
class MyRunnable implements Runnable
{ String Myname;
MyRunnable ()
{ Myname =“MyRunnable”;
}
void run()
{ Scanner s=new Scanner(System.in);
SOP(“enter name”);
name=s.next();
} }
Class TheradDemo
{
PSVM(String args[])
{ MyRunnable Thread t=new MyRunnable();
Thread t=new Thread(r);
t.start();
}}
Making Thread
Two type ways creating thread
Runnable
Class named as Thread
To implement Runnable interface
By extending the Thread class
Example
Extending the Thread class :
WAP demonstrate The making of thread to print number from 1 to
10 by extending the “Thread” Class .
import java.io.*; class Main
class Numbers extends Thread {
{ public void run() psvm(String args[])
{ {
int i; Thread t1=new Number();
for(i=1;i<=10;i++) t1.start();
{ }
System.out.println(i); }
} }}
1 5
2 7
3 8
4 9
10
To implement Runnable interface .
WAP demonstrate The making of thread to print number from 1 to
10.
import java.io.*; class Main
class Numbers implements Runnable {
{ psvm(String args[])
public void run() {
{
int i; Numbers n=new Numbers();
for(i=1;i<=10;i++) Thread t1=new Thread(n);
{ t1.start();
System.out.println(i); }
}}} }
1 5
2 7
3 8
4 9
10
Creating Multiple Threads :
WAP that has two threads. one of the threads
display the odd numbers from 1 to 10 while the
other displays even numbers from 1 to 10.use the
sleep() method to display the number sequentially
Creating Multiple Threads :
WAP that has two threads .one of the threads display the odd
numbers from 1 to 10 while the other displays even numbers from 1
to 10.
import java.io.*; class Main
class OddNumbers extends Thread {
{ PSVM(String args[])
public void run() {
{ int i; OddNumbers n=new
for(i=1;i<=10;i+=2) OddNumbers();
{ Thread t1=new Thread(n);
System.out.println(i); } EvenNumbers n1=new
}} EvenNumbers();
class EvenNumbers extends Thread Thread t2=new Thread(n1);
{ public void run() t1.start();
{ int i; t2.start();}
for(i=2;i<=10;i+=2) }
{
System.out.println(i);
}
}}
Thread Life Cycle
Start the thread
Sleep for specified time

Join ()
Yields control to
Wait for other to another thread
Finesh then join!

You might also like