public static void main(String[] args) throws InterruptedException {
sleepMethod();
//waitMethod();
}
private static void sleepMethod() throws InterruptedException {
for (int i = 1; i <= 5; i++) {
System.out.println("Hello wor1d");
Thread.sleep(1000);
}
}
public static synchronized void waitMethod() throws InterruptedException {
for (int i = 1; i <= 5; i++) {
System.out.println("Hel1o world");
Demo_Thread1.class.wait(1000);
}
}
public class Demo2_Thread {
public static void main(String[] args) {
final Printer p = new Printer();
new Thread(){
@Override
public void run(){
while (true) {
try {
p.print1();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
new Thread(){
@Override
public void run(){
while (true) {
try {
p.print2();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class Printer{
int flag = 1;
public void print1() throws InterruptedException {
synchronized (Printer.class){
if (flag != 1){
Printer.class.wait();
}
System.out.println("alicyu.com");
flag = 2;
Printer.class.notify();
}
}
public void print2() throws InterruptedException {
synchronized (Printer.class){
if (flag != 2){
Printer.class.wait();
}
System.out.println("www.alicyu.com");
flag = 1;
Printer.class.notify();
}
}
}
public class Demo2_Thread {
public static void main(String[] args) {
final Printer p = new Printer();
new Thread(){
@Override
public void run(){
while (true) {
try {
p.print1();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
new Thread(){
@Override
public void run(){
while (true) {
try {
p.print2();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
new Thread(){
@Override
public void run(){
while (true) {
try {
p.print3();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class Printer{
int flag = 1;
public void print1() throws InterruptedException {
synchronized (Printer.class){
if (flag != 1){
Printer.class.wait();
}
System.out.println("alicyu.com");
flag = 2;
Printer.class.notify();
}
}
public void print2() throws InterruptedException {
synchronized (Printer.class){
if (flag != 2){
Printer.class.wait();
}
System.out.println("www.alicyu.com");
flag = 3;
Printer.class.notify();
}
}
public void print3() throws InterruptedException {
synchronized (Printer.class){
if (flag != 3){
Printer.class.wait();
}
System.out.println("std.alicyu.com");
flag = 1;
Printer.class.notify();
}
}
}
class Printer{
int flag = 1;
public void print1() throws InterruptedException {
synchronized (Printer.class){
while (flag != 1){
Printer.class.wait();
}
System.out.println("alicyu.com");
flag = 2;
Printer.class.notify();
}
}
public void print2() throws InterruptedException {
synchronized (Printer.class){
while (flag != 2){
Printer.class.wait();
}
System.out.println("www.alicyu.com");
flag = 3;
Printer.class.notify();
}
}
public void print3() throws InterruptedException {
synchronized (Printer.class){
while (flag != 3){
Printer.class.wait();
}
System.out.println("std.alicyu.com");
flag = 1;
Printer.class.notify();
}
}
}
本文由 Alicyu 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
原文链接:https://www.alicyu.com/archives/thread-wait-sleep
最后更新:2019-10-18 14:37:58
Update your browser to view this website correctly. Update my browser now