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

Java Assigment

The document discusses multiple threads and multithreading in Java. It shows examples of creating new threads that print output, changing a thread name, and threads running concurrently and outputting numbers.

Uploaded by

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

Java Assigment

The document discusses multiple threads and multithreading in Java. It shows examples of creating new threads that print output, changing a thread name, and threads running concurrently and outputting numbers.

Uploaded by

abc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q-1

(i) My thread is in running state.


(ii)
Current Thread main

Name main

Changed Name New Thread

this thread will print first 10 numbers

(iii)
Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java

(iv)
New thread: One

New thread: Two

New thread: Three

Two: 5

One: 5

Three: 5

Two: 4

One: 4

Three: 4

Two: 3
One: 3

Three: 3

Two: 2

Three: 2

One: 2

Two: 1

Three: 1

One: 1

Two exiting.

Three exiting.

One exiting.

Main thread exiting.

Q-2
class Mthreading extends Thread {

public void run()

try {

System.out.println(

"Thread " + Thread.currentThread().getId() + " is running");

catch (Exception e) {

System.out.println("Exception is caught");

class Multithread {

public static void main(String[] args) {

int n = 8;

for (int i = 0; i < n; i++) {

Mthreading obj = new Mthreading();

obj.start();

}
}

Q-3

class creatingthread extends Thread

public int name,i;

public void run()

for(i=1;i<=50;i++)

System.out.println("Thread" + name + " : " + i);

try

sleep(1000);

catch(Exception e)

System.out.println("some problem");

}
class test

public static void main(String[] args)

creatingthread obj1 = new creatingthread();

creatingthread obj2 = new creatingthread();

obj1.name=1;

obj2.name=2;

obj1.start();

obj2.start();

You might also like