package cd_one.code12.runnablesafe;
class SaleTicket1 implements Runnable{
int ticket = 100;
boolean isFlag = true;
@Override
public void run() {
while(isFlag){
show();
}
}
public synchronized void show(){
if(ticket > 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket);
ticket--;
}else{
isFlag = false;
}
}
}
public class WindowTest1 {
public static void main(String[] args) {
SaleTicket1 s = new SaleTicket1();
Thread t1 = new Thread(s);
Thread t2 = new Thread(s);
Thread t3 = new Thread(s);
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
![](https://img-blog.csdnimg.cn/direct/eeeebea1d3234756bd344790557a13e4.png)
package cd_one.code12.exer7;
class Window1 extends Thread{
static int ticket = 100;
static boolean isFlag = true;
static Object obj = new Object();
@Override
public void run() {
while(true){
show();
}
}
public static synchronized void show(){
if(ticket > 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace ();
}
System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket);
ticket--;
}else{
isFlag = false;
}
}
}
public class WindowTest1 {
public static void main(String[] args) {
Window1 w1 = new Window1();
Window1 w2 = new Window1();
Window1 w3 = new Window1();
w1.setName("窗口1");
w2.setName("窗口2");
w3.setName("窗口3");
w1.start();
w2.start();
w3.start();
}
}
![](https://img-blog.csdnimg.cn/direct/fb19074dedb0465eb1a2993d61fe0090.png)
package cd_one.code12.exer6;
class Window extends Thread{
static int ticket = 100;
static Object obj = new Object();
@Override
public void run() {
while(true){
synchronized (Window.class){
if(ticket > 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket);
ticket--;
}else{
break;
}
}
}
}
}
public class windowTest {
public static void main(String[] args) {
Window w1 = new Window();
Window w2 = new Window();
Window w3 = new Window();
w1.setName("窗口1");
w2.setName("窗口2");
w3.setName("窗口3");
w1.start();
w2.start();
w3.start();
}
}
![](https://img-blog.csdnimg.cn/direct/c094515f73cb4732ab08977340cd0528.png)
![](https://img-blog.csdnimg.cn/direct/4a699663abba489987eda28a8b411e52.png)
![](https://img-blog.csdnimg.cn/direct/1473f52aa8624756bcdb01a028093baf.png)
package cd_one.code12;
public class HappyNewYear {
public static void main(String[] args) {
for (int i = 10; i >= 0 ; i--) {
if(i == 0){
System.out.println("Happy New Year");
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
}
![](https://img-blog.csdnimg.cn/direct/364b19559fc349f8ab91908c54f481a2.png)
![](https://img-blog.csdnimg.cn/direct/a4fd1e605632468da6824b372fab9fbc.png)
package cd_one.code12.exer8;
public class AccountTest {
public static void main(String[] args) {
Account acct = new Account();
Customer customer1 = new Customer(acct,"甲");
Customer customer2 = new Customer(acct,"乙");
customer1.start();
customer2.start();
}
}
class Account{
private double balance;
public synchronized void deposit(double amt){
if(amt > 0){
balance += amt;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"存钱1000块,余额为:" +balance);
}
}
class Customer extends Thread{
Account account;
public Customer(Account acct){
this.account = acct;
}
public Customer(Account acct,String name){
super(name);
this.account = acct;
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
account.deposit(1000);
}
}
}
![](https://img-blog.csdnimg.cn/direct/00be972a85b345fe8402228e6a4cff95.png)
package cd_one.code12.exer8;
public class AccountTest {
public static void main(String[] args) {
Account acct = new Account();
Customer customer1 = new Customer(acct,"甲");
Customer customer2 = new Customer(acct,"乙");
customer1.start();
customer2.start();
}
}
class Account{
private double balance;
public synchronized void deposit(double amt){
if(amt > 0){
balance += amt;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"存钱1000块,余额为:" +balance);
}
}
class Customer extends Thread{
Account account;
public Customer(Account acct){
this.account = acct;
}
public Customer(Account acct,String name){
super(name);
this.account = acct;
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
account.deposit(1000);
}
}
}
![](https://img-blog.csdnimg.cn/direct/f46dc2233a0542fb814d3fddc97eb374.png)