62,614
社区成员
发帖
与我相关
我的任务
分享
package com.test;
class NumThread1 extends Thread {
public void run() {
for(int i=1;i<=26;i++){
System.out.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class BigCharThread extends Thread{
private Thread person;
public BigCharThread(Thread person) {
this.person=person;
}
public void run() {
try {
person.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i='A';i<='Z';i++){
System.out.println((char)i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class SmallCharThread extends Thread{
private Thread person;
public SmallCharThread(Thread person) {
this.person=person;
}
public void run() {
try {
person.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i='a';i<='z';i++){
System.out.println((char)i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class NumChar {
public static void main(String[] args) {
NumThread1 n=new NumThread1();
BigCharThread b=new BigCharThread(n);
SmallCharThread s=new SmallCharThread(b);
n.start();
s.start();
b.start();
}
}
package com.test;
class NumberThread extends Thread {
public void run() {
for (int i = 1; i <= 26; i++) {
System.out.println(i);
}
}
}
class BigCharThread extends Thread {
public void run() {
for (int i = 'A'; i <= 'Z'; i++) {
System.out.println((char) i);
}
}
}
class SmallCharThread extends Thread {
public void run() {
for (int i = 'a'; i <= 'z'; i++) {
System.out.println((char) i);
}
}
}
public class ThreeThread {
public static void main(String[] args) {
NumberThread num = new NumberThread();
BigCharThread big = new BigCharThread();
SmallCharThread small = new SmallCharThread();
num.start();
big.start();
small.start();
}
}
class ThreadNode
{
boolean hasToken = false;//true if this is first Thread
Node next;//need initialized
synchronized void waitToken() throws InterruptedException
{
while(!hasToken)
this.wait();
}
void switchNext()
{
this.hasToken = false;
next.setToken();
}
synchronized void setToken()
{
this.hasToken = true;
this.notifyAll();
}
public void loop() throws InterruptedException
{
for(;;)
{
this.waitToken();
//do my jobs
this.switchNext();
}
}
}
package com.test;
class NumberThread extends Thread {
public void run() {
for (int i = 1; i <= 26; i++) {
System.out.println(i);
}
}
}
class BigCharThread extends Thread {
public void run() {
for (int i = 'A'; i <= 'Z'; i++) {
System.out.println((char) i);
}
}
}
class SmallCharThread extends Thread {
public void run() {
for (int i = 'a'; i <= 'z'; i++) {
System.out.println((char) i);
}
}
}
public class ThreeThread {
public static void main(String[] args) {
NumberThread num = new NumberThread();
BigCharThread big = new BigCharThread();
SmallCharThread small = new SmallCharThread();
num.start();
big.start();
small.start();
}
}
package hr.test;
//临界区,用于控制打印线程的工作顺序和互斥操作
class Mutex{
private int order=1; //记录线程顺序号
private int maxNum=0; //最大线程数
Mutex(int num){
this.maxNum=num;
}
//循环设置下一次要打印的顺序号
public void setOrder(){
this.order=(++order)<=maxNum?order:1;
}
//得到顺序号
public int getOrder(){
return this.order;
}
}
//打印工作
class PrintRunnable implements Runnable{
private Object[] cont=null; //打印内容
private int order=-1; //当前线程的打印顺序
private Mutex mutex=null; //临界区对象
PrintRunnable(Object[] c,int o,Mutex m){
this.cont=c;
this.order=o;
this.mutex=m;
}
public void run(){
try{
//循环打印内容
for(Object c:cont){
//互斥操作临界区
synchronized(mutex){
//如果当前工作线程的顺序号不等于临界区的顺序号,则工作线程等待阻塞,并放弃锁
while(mutex.getOrder()!=this.order)
mutex.wait();
//打印内容
System.out.println(c);
//设置下一次允许打印的线程顺序
mutex.setOrder();
//唤醒在临界区等待的所有线程
mutex.notifyAll();
}
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
//测试
public class Test {
public static void main(String[] args){
Integer[] number={1,2,3,4};
Character[] upperChar={'A','B','C','D'};
Character[] lowerChar={'a','b','c','d'};
Mutex mutex=new Mutex(3);
new Thread(new PrintRunnable(number,1,mutex)).start();
new Thread(new PrintRunnable(upperChar,2,mutex)).start();
new Thread(new PrintRunnable(lowerChar,3,mutex)).start();
}
}