Home > Back-end >  Consult the rewrite lambda expressions
Consult the rewrite lambda expressions

Time:10-14

 public class lazyMan {

Private lazyMan () {
System. Out. Println (Thread. CurrentThread (). The getName () + "OK");
}

Private static lazyMan lazyMan;

Public static lazyMan getInstance () {
If (lazyman==null) {
Lazyman=new lazyman ();
}
Return lazyman;
}

Public static void main (String [] args) {
for(int i=0; i<10; I++) {
New Thread (() - & gt; {
LazyMan. GetInstance ();
}).start();
}
}
}

The
 new Thread (() - & gt; {
LazyMan. GetInstance ();
}).start();
to the lambda expressions, how to change?

CodePudding user response:

New Thread () {
@ Override
Public void the run () {
LazyMan. GetInstance ();
}
}. The start ();

CodePudding user response:

Public class lazyManThread extends Thread {
@ Override
Public void the run () {
LazyMan. GetInstance ();
}
}

Modify the main method under
LazyManThread thread=new lazyManThread ();
Thread. The run ();

CodePudding user response:

New Thread () {
@ Override
Public void the run () {
LazyMan. GetInstance ();
}
}. The start ();

CodePudding user response:

refer to the second floor KeepSayingNo response:
public class lazyManThread extends Thread {
@ Override
Public void the run () {
LazyMan. GetInstance ();
}
}

Modify the main method under
LazyManThread thread=new lazyManThread ();
Thread. The run ();


This is jdk1.8 code above no problem, but still have problem, instead of 1.6 thread under the lambda also please advise of

reference 1/f O reply: happiness is
new Thread () {
@ Override
Public void the run () {
LazyMan. GetInstance ();
}
}. The start ();


This is jdk1.8 code above no problem, but still have problem, instead of 1.6 lambda also please advice more threads

CodePudding user response:

The
refer to the original poster flyskytoday response:
 public class lazyMan {

Private lazyMan () {
System. Out. Println (Thread. CurrentThread (). The getName () + "OK");
}

Private static lazyMan lazyMan;

Public static lazyMan getInstance () {
If (lazyman==null) {
Lazyman=new lazyman ();
}
Return lazyman;
}

Public static void main (String [] args) {
for(int i=0; i<10; I++) {
New Thread (() - & gt; {
LazyMan. GetInstance ();
}).start();
}
}
}

The
 new Thread (() - & gt; {
LazyMan. GetInstance ();
}).start();
to the lambda expressions, how to change?


 
New Thread (new Runnable () {
@ Override
Public void the run () {
//TODO Auto - generated method stub
LazyMan. GetInstance ();
}
}).start();
  • Related