package thread;
class Coutalphabet extends Thread{
public void run() {
for(int i = 'A';i<='Z';i++)
{
char c = (char)i;
System.out.print(" "+c);
}
}
}
class CoutGreekalphabet {
public void run() {
for(int i = 'α';i<='α'+24;i++ )
{
char c = (char)i;
System.out.print(" "+c);
}
}
}
class first {
public static void main(String args[]) {
Coutalphabet coutalphabet;
CoutGreekalphabet coutgreekalphabet;
coutalphabet = new Coutalphabet();
coutalphabet.start();
coutgreekalphabet = new CoutGreekalphabet();
coutgreekalphabet.run();
}
}
package tt;
class ClassRoom implements Runnable{
Thread teacher;
Thread student1 ;
Thread student2 ;
ClassRoom(){
teacher = new Thread(this);
student1 = new Thread(this);
student2 = new Thread(this);
teacher.setName("老师");
student1.setName("李四");
student2.setName("王五");
}
@Override
public void run() {
if(Thread.currentThread() == teacher)
{
try {Thread.sleep(100);
for(int i=1 ;i <=3;i++)
{
System.out.println("上课!");
}
}
catch(InterruptedException e)
{
}
student1.interrupt();
}
if(Thread.currentThread() == student1)
{
try {
System.out.println("我李四要睡10分钟的觉,睡觉真香!");
Thread.sleep(1000*60*10);
}
catch(InterruptedException e)
{
System.out.println("我李四竟然被老师吵醒了,悄咪咪我也把王五叫起来哈哈哈!");
}
student2.interrupt();
}
if(Thread.currentThread() == student2)
{
try {
System.out.println("我王五要睡李四的五倍,睡觉香!");
Thread.sleep(1000*60*50);
}
catch(InterruptedException e)
{
System.out.println("李四竟然把我吵醒了!啊,原来要上课了!");
}
}
}
}
public class first {
public static void main(String args[]) throws InterruptedException
{
ClassRoom classroom = new ClassRoom();
classroom.teacher.start();
classroom.student1.start();
classroom.student2.start();
}
}
package Afirst;
class Coutalphabet extends Thread{
public void run(){
if(Thread.currentThread().getName().equals("coutalphabet"))
{
print();
}
else if(Thread.currentThread().getName().equals("coutgreekalphabet")) {
print();
}
}
public synchronized void print() {
if(Thread.currentThread().getName().equals("coutalphabet"))
{ for(int i = 'A';i<='Z';i++)
{
char c = (char)i;
System.out.print(" "+c);
}
}
else if(Thread.currentThread().getName().equals("coutgreekalphabet"))
{
try {
Thread.sleep(100);
for(int i = 'α';i<='α'+24;i++ )
{
char c = (char)i;
System.out.print(" "+c);
}
}
catch(InterruptedException e) {
}
}
}
}
public class Afirst {
public static void main(String args[]) throws InterruptedException
{
Coutalphabet coutalphabet = new Coutalphabet();
Coutalphabet coutgreekalphabet =new Coutalphabet();
coutalphabet.setName("coutalphabet");
coutgreekalphabet.setName("coutgreekalphabet");
coutalphabet.start();
coutgreekalphabet.start();
}
}
package Aticket;
class TicketHouse implements Runnable {
int moneyfive = 1;
int moneyten = 0;
int moneytwenty = 0;
@Override
public void run() {
if (Thread.currentThread().getName().equals("张某")) {
saleTicket(20);
} else if (Thread.currentThread().getName().equals("孙某")) {
saleTicket(10);
} else if (Thread.currentThread().getName().equals("赵某")) {
saleTicket(5);
}
}
public synchronized void saleTicket(int money) {
if (money == 10 && moneyfive >= 1) {
moneyten += 1;
try {
Thread.sleep(100);
System.out.println("给" + Thread.currentThread().getName() + "入场券" + "!还给您5元");
} catch (InterruptedException e) {
}
} else if (money == 5) {
moneyfive += 1;
try {
Thread.sleep(50);
System.out.println("给" + Thread.currentThread().getName() + "入场券" + ",钱刚好够");
} catch (InterruptedException e) {
}
} else if (money == 20) {
while ( moneyten < 1&&money>=2) {
try {
System.out.println("\n" + Thread.currentThread().getName() + "靠边等...");
wait();
System.out.println("\n" + Thread.currentThread().getName() + "继续买票");
} catch (InterruptedException e) {
}
}
if (moneyfive >= 3 || moneyten >= 1) {
moneyfive -= 3;
moneytwenty += 1;
System.out.println("给" + Thread.currentThread().getName() + "入场券," + Thread.currentThread().getName()
+ "给20,找回15元");
}
}
notifyAll();
}
}
public class Aticket {
public static void main(String args[]) {
TicketHouse officer = new TicketHouse();
Thread Sun = new Thread(officer);
Thread Zhang = new Thread(officer);
Thread Zhao = new Thread(officer);
Sun.setName("孙某");
Zhang.setName("张某");
Zhao.setName("赵某");
Sun.start();
Zhang.start();
Zhao.start();
}
}