0% found this document useful (0 votes)
7 views6 pages

Thread Lab

The document contains multiple Java programs demonstrating the use of threads, including extending the Thread class and implementing the Runnable interface. Various methods such as start(), run(), and yield() are showcased, along with thread naming and interaction between multiple threads. Each program illustrates different threading concepts and behaviors in Java.

Uploaded by

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

Thread Lab

The document contains multiple Java programs demonstrating the use of threads, including extending the Thread class and implementing the Runnable interface. Various methods such as start(), run(), and yield() are showcased, along with thread naming and interaction between multiple threads. Each program illustrates different threading concepts and behaviors in Java.

Uploaded by

aks12399662
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 1:

class MyThread extends Thread{


public void run(){
[Link]("no argument method");
}
public void run(int x){
[Link]("argument method");
}
}
class ThreadDemo1{
public static void main(String args[]){
MyThread t=new MyThread();
[Link]();
}
}
--------------------------
Program 2:
class MyThread extends Thread{
public void run(){
[Link]("no argument method");
}
public void run(int x){
[Link]("argument method");
}
}
class ThreadDemo2{
public static void main(String args[]){
MyThread t=new MyThread();
[Link]();
}
}
Program 3:
class MyRunnable implements Runnable{
public void run(){
for( int i=0;i<10;i++)
[Link]("Child Thread");
}
}
class ThreadDemo2{
public static void main(String args[]){
MyRunnable r=new MyRunnable();
Thread t=new Thread(r);
[Link]();
for(int i=0;i<10;i++)
[Link]("main thread");
}
}

Program 4:
class MyThread extends Thread{
public void start(){
[Link]("start method");
}
public void run(){
[Link]("run method");
}
}
class ThreadDemo3{
public static void main(String args[]){
MyThread t=new MyThread();
[Link]();
[Link]("main method");
}
}
-------------------
Program 5:
class MyThread extends Thread{
public void start(){
[Link]();
[Link]("start method");
}
public void run(){
[Link]("run method");
}
}
class ThreadDemo4{
public static void main(String args[]){
MyThread t=new MyThread();
[Link]();
[Link]("main method");
}
}

Program 6:
class ThreadDemo8{
public static void main(String args[]){
[Link]("name of current thread:"+[Link]().getName());
MyThread t=new MyThread();
[Link]("name of child thread:"+[Link]());
[Link]().setName( "cse");
[Link]("now name of current thread:"+[Link]().getName());
[Link]();
}
}
-------------------------------------------------
Program 7:
class A extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
if (i == 1)
[Link]();
[Link]("\tFrom Thread A : i = " + i);
}
[Link]("Exit from A");
}
}

class B extends Thread {


public void run() {
for (int j = 1; j <= 5; j++) {
[Link]("\tFrom Thread B : j = " + j);
if (j == 3) stop();
}
[Link]("Exit from B");
}
}

class C extends Thread {


public void run() {
for (int k = 1; k <= 5; k++) {
[Link]("\tFrom Thread C : k = " + k);
if (k == 1)
try {
sleep(10000);
} catch (Exception e) {
}
}
[Link]("Exit from C");
}
}

class ThreadMethods {
public static void main(String args[]) {
A threadA = new A();
B threadB = new B();
C threadC = new C();

[Link]("Start thread A");


[Link]();

[Link]("Start thread B");


[Link]();

[Link]("Start thread C");


[Link]();

[Link]("End of main thread");


}
}
-------------------------------------
Program 8:
class A extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("\tFrom Thread A : i = " + i);
}
[Link]("Exit from A");
}
}

class B extends Thread {


public void run() {
for (int j = 1; j <= 5; j++) {
[Link]("\tFrom Thread B : j = " + j);
}
[Link]("Exit from B");
}
}

class C extends Thread {


public void run() {
for (int k = 1; k <= 5; k++) {
[Link]("\tFrom Thread C : k = " + k);
}
[Link]("Exit from C");
}
}

class ThreadTest {
public static void main(String args[]) {
new A().start();
new B().start();
new C().start();
}
}

Program 9:
class MyThread extends Thread{
public void run(){
for(int i=0;i<10;i++){
[Link]("child thread");
[Link]();
}
}
}
class ThreadYieldDemo{
public static void main(String args[]){
MyThread t=new MyThread();
[Link]();
for(int i=0;i<10;i++){
[Link]("parent thread");
}
}
}

You might also like