This is my code which i tried but my main class is not there because i don't know how to use that one
//first thread
class firstthread extends Thread
{
public void run(){
for(int i=0;i<1000;i++)
{
System.out.println(i);
}}
}
//second thread
class secondthread extends Thread
{
public void run(){
for(int i=0;i<1000;i++)
{
System.out.println(i);
}}
}
First overide the run method and then create the object of thread class in main()
and call start method.
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
new Thread() {
public void run() {
for(int y=0;y<1000;y++)
{
System.out.println(y);
}
};
}.start();
}
}
Whatever you have written is incomplete code, to create a thread you need to extend Thread class or implement Runnable interface and then override its public void run() method.
To create a thread you need to override the method public void run
Then to start the threads you need to call its start() method.
A simple complete example
class MyThread extends Thread {
String name;
public void run(){
for(int i=0;i<1000;i++) {
System.out.println("Thread name :: "+name+" : "i);
}
}
}
class Main{
public static void main(String args[]){
MyThread t1 = new MyThread();
t1.name = "Thread ONE";
MyThread t2 = new MyThread();
t2.name = "Thread TWO";
MyThread t3 = new MyThread();
t3.name = "Thread THREE";
t1.start();
t2.start();
t3.start();
}
}
You can't just put some code in your class body.
You need a method to have the code in, the method being run() in case of thread.
Instead of copy-pasting the code, I'll point you to the official documentation where you can find some examples.
Sample program given below. Since there is no synchronization code, there output is mixed from the three threads
public class ThreadTest implements Runnable{
#Override
public void run() {
System.out.print(Thread.currentThread().getId() + ": ");
for(int i=0;i<100;i++)
System.out.print(i + ", ");
System.out.println();
}
public static void main(String[] args) {
for(int i=0;i<3;i++){
new Thread(new ThreadTest()).start();
}
}
}
Related
Here is my simple code for 2 threads to alternate in printing 2 different numbers. Thread A prints 1 and Thread B prints 2. Both should alternate amongst themselves to print 1,2,1,2,1,2,1,2,....and so on.
Now I see in the below program we are getting <1,2>,<1,2> combination,...and so on for 5 times only.
Looks like both threads have the same variables which is being shared. But i is a local variable. How it i shared
public class MyThread extends Thread{
int numToPrint;
Turn turn;
MyThread(int n, Turn turn)
{
this.numToPrint=n;
this.turn=turn;
}
public void run() {
for (int i=0;i<10;i++) {
if(turn.t==numToPrint) {
System.out.println(numToPrint);
if(numToPrint==1)
turn.t=2;
else
turn.t=1;
turn.doNotify();
}
else
{
turn.doWait();
}
}
}
}
public class Main {
public static void main(String[] args) {
Turn t=new Turn();
MyThread t1=new MyThread(1,t);
t1.start();
MyThread t2=new MyThread(2,t);
t2.start();
}
}
how to run threads after other threads finished, let say i have 3 java class (Cls1 and Cls2 implements runnable, and I use sleep to know which statements are run first), this is my code :
public class Master {
#SuppressWarnings("unused")
public static void main(String[] args) {
//loop1
for(int i=1; i<=2; i++) {
Cls1 c1 = new Cls1();
}
//Here i want to wait until the thread loop1 is finished, what to do?
//loop2
for(int j=1; j<=2; j++) {
Cls2 c2 = new Cls2();
}
}
}
public class Cls1 implements Runnable{
Thread myThread;
Cls1() {
myThread = new Thread(this, "");
myThread.start();
}
#Override
public void run() {
System.out.println("hello1");
TimeUnit.SECONDS.sleep(3);
System.out.println("hello2");
}
}
public class Cls2 implements Runnable{
Thread myThread;
Cls2() {
myThread = new Thread(this, "");
myThread.start();
}
#Override
public void run() {
System.out.println("hello3");
TimeUnit.SECONDS.sleep(3);
System.out.println("hello4");
}
}
And this is output my code :
hello1
hello1
hello3
hello3
hello2
hello2
hello4
hello4
And this is the output I expect:
hello1
hello1
hello2
hello2
hello3
hello3
hello4
hello4
What should I do ?
You could try something like that:
#SuppressWarnings("unused")
public static void main(String[] args) {
Thread threads[] = new Thread[2];
//loop1
for(int i=1; i<=2; i++) {
threads[i-1] = new Cls1();
}
for (Thread thread: threads) {
thread.join();
}
//loop2
for(int j=1; j<=2; j++) {
Cls2 c2 = new Cls2();
}
}
UPDATE: Make Cls1 a subclass of Thread:
public class Cls1 extends Thread {
Cls1() {
start();
}
#Override
public void run() {
System.out.println("hello1");
TimeUnit.SECONDS.sleep(3);
System.out.println("hello2");
}
}
If you want to wait for the thread to finish, call the join method.
To run thread one after another, it needs to be synchronized.
wait, notify and notifyAll ..all of these can be used.
If you don't synchronize it, then it doesn't guarantee the order of output what you desire to be produced.
So for this we have to take one variable "flag" and and synchronize threads one by one as below:
If value of flag=1, then it is class A's turn to print.
If value of flag=2, then it is class B's turn to print.
If value of flag=3, then it is class C's turn to print.
I don't have much experience with threading in Java. How can I get the value of String ft all way from inside Runnable(){} to use it in main().
public class testInner {
public static void main (String[] args){
Outer out =new Outer();
out.makeinner();
System.out.println("main :" + out.getft());
}
}
public class Outer {
private volatile String ft;
public String getft(){
return ft;
}
public void makeinner(){
inner in = new inner();
in.changeOuter();
}
public class inner{
public void changeOuter(){
new Thread(new Runnable(){
public void run(){
ft = "what?";
System.out.println("run :" + ft);
}
}).start();
}
}
}
Result when compiled:
main :null
run :what?
Threads are executed in parallel, so you can't be sure the thread has been successfully executed when you do System.out.println("main :" + out.getft());. This is why you get null, the thread didn't edit that variable yet.
If you want to be sure the thread finishes the execution, use join:
public class inner{
public void changeOuter(){
Thread myThread = new Thread(new Runnable(){
public void run(){
ft = "what?";
System.out.println("run :" + ft);
}
});
myThread.start();
myThread.join(); // wait for the thread to finish before returning
}
}
Please see the program below
public class TestVolatile implements Runnable {
public static volatile int counter;
public static String lock = "lock";
public static void main(String[] args) {
Thread t1 = new Thread(new TestVolatile(),"Thread-1");
Thread t2 = new Thread(new TestVolatile(),"Thread-2");
t1.start();
t2.start();
}
public void run() {
synchronized(this) {
System.out.println(Thread.currentThread()+"-"+counter);
counter++;
}
}
}
If I run this program multiple times, I get 3 different results.
first is
Thread[Thread-1,5,main]-0
Thread[Thread-2,5,main]-0
second is
Thread[Thread-1,5,main]-0
Thread[Thread-2,5,main]-1
third is
Thread[Thread-1,5,main]-1
Thread[Thread-2,5,main]-0
But if change the lock object from "this" to "lock", I get 2 different results
first is
Thread[Thread-1,5,main]-0
Thread[Thread-2,5,main]-1
second is
Thread[Thread-1,5,main]-1
Thread[Thread-2,5,main]-0
My assumption when writing the program was that in either case the "counter" should never come 0 in both statements.
Can somebody explain?
You create two TestVolatile objects. The "this" keyword refers to the TestVolatile object being run in the thread. Thus you do not synchronize on the same object in the first example.
If you change the code like this, then the first example starts working:
public static void main(String[] args) {
TestVolatile testVolatile = new TestVolatile();
Thread t1 = new Thread(testVolatile,"Thread-1");
Thread t2 = new Thread(testVolatile,"Thread-2");
t1.start();
t2.start();
}
It's probably not what you're looking for, but if you want to avoid the use of synchronized and volatile, you should use an instance of AtomicInteger:
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicInteger.html
Use the getAndIncrement method to show the same behavior as in your example.
public class TestVolatile implements Runnable {
public static AtomicInteger counter = new AtomicInteger();
public static void main(String[] args) {
Thread t1 = new Thread(new TestVolatile(),"Thread-1");
Thread t2 = new Thread(new TestVolatile(),"Thread-2");
t1.start();
t2.start();
}
public void run() {
System.out.println(Thread.currentThread() + " - " + counter.getAndIncrement());
}
}
I believe variables used in static main method should be also static as well.
The problem is that I cannot use this in this method at all. If I remember correctly, I have to initiate thread with commnad myThread = new ThreaD(this).
The below codes produces an error because I used this in thread initiation.
What have I done wrong here?
package app;
public class Server implements Runnable{
static Thread myThread;
public void run() {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
System.out.println("Good morning");
myThread = new Thread(this);
}
}
You can't use this because main is a static method, this refers to the current instance and there is none. You can create a Runnable object that you can pass into the thread:
myThread = new Thread(new Server());
myThread.start();
That will cause whatever you put in the Server class' run method to be executed by myThread.
There are two separate concepts here, the Thread and the Runnable. The Runnable specifies what work needs to be done, the Thread is the mechanism that executes the Runnable. Although Thread has a run method that you can extend, you can ignore that and use a separate Runnable.
Change new Thread(this) to new Thread(new Server()):
package app;
public class Server implements Runnable{
static Thread myThread;
public void run() {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
System.out.println("Good morning");
myThread = new Thread(new Server());
}
}
class SimpleThread extends Thread {
public SimpleThread(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " thread: " + getName());
try {
sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {}
}
System.out.println("DONE! thread: " + getName());
}
}
class TwoThreadsTest {
public static void main (String[] args) {
new SimpleThread("test1").start();
new SimpleThread("test2").start();
}
}