希望大家给出自己的解答,大家相互学习。
解决方案 »
- JTree修改当前节点时,怎样获取修改后的节点名称?
- in.readLine读入的跟 定义的字符串字面不相同
- 初学,,帮帮忙, 各位大虾,叔叔伯伯阿姨婶婶们.
- 高手快来解决一个关于字符串的问题!!急
- http://www.2qq.cn/vip.htm?qq=282232 腾讯过大年,六位数的QQ不要要钱,点击马上申请!看看有分。呵呵,
- [救命] 50分, 求好用的JAVA 编译器
- 请问如何配置J2SE 1.4.2?
- 参数传递的问题!(大家讨论讨论)
- 这个异常为什么会出现,具体见内容。请帮忙
- ◎◎◎急急急!!js为什么没有执行?◎◎◎
- AES解密问题,C++写的加密,php类库可以解,java的只能解出一部分
- java3d中detach问题
public class Test {
static class Utility {
private String turn = "A";//这样可以保证A最先被打印
public synchronized void printName() {
int cnt = 0;
String curThreadName = Thread.currentThread().getName();
for(int i=0; i<20; i++) {
while(!curThreadName.equals(turn))//当该线程进入同步方法并且轮到它打印的时候才打印,否则释放锁,进入休息室等待去
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(curThreadName);
cnt++;
if(curThreadName.equals("C") && cnt%2 == 0) {
System.out.println();
}
if(curThreadName.equals("A")) {//让它们交替打印
turn = "B";
} else if(curThreadName.equals("B")) {
turn = "C";
} else {
turn = "A";
}
notifyAll();//完成了动作,通知休息室中的线程进来
}
}
}
static class Runner extends Thread {
private Utility mutex;
public Runner(Utility mutex, String name) {
super(name);
this.mutex = mutex;
} public void run() {
mutex.printName();
}
} public static void main(String[] args) throws Exception {
Utility mutex = new Utility();
Thread t1 = new Runner(mutex, "A");
Thread t2 = new Runner(mutex, "B");
Thread t3 = new Runner(mutex, "C");
t1.start();
t2.start();
t3.start();
}
}
private static final String[] ref = {"A", "B", "C"};
private int count = 10;
private StringBuffer currentString;
public TestThread(String[] ref, StringBuffer shareString){
this.setName(ref[(int) (this.getId() % ref.length)]);
this.currentString = shareString;
}
@Override
public void run() {
while (count > 0){
synchronized(currentString){
if (currentString.toString().equals( this.getName())){
System.out.print(this.getName());
currentString.replace(0,
currentString.length(),
TestThread.nextInRef(currentString.toString()));
count-=1;
}
}
}
}
static public String nextInRef(String currentString){
for (int i = 0; i < ref.length; i++){
if (ref[i].equals(currentString)){
return ref[(i+1)%ref.length];
}
}
return ref[0];
}
public static void main(String[] arg){
StringBuffer shareString = new StringBuffer(ref[0]);
for (int i = 0; i < ref.length; i++){
new TestThread(ref, shareString).start();
}
}
}
package com.testClass;public class TestPrintID {
public static void main(String args[]) {
Object a = new Object();
Object b = new Object();
Object c = new Object(); Print pA = new Print("A", b, a);
Print pB = new Print("B", a, c);
Print pC = new Print("C", c, b);
pA.start();
pB.start();
pC.start();
}
}class Print extends Thread {
Object pre;
Object self; public Print(String name, Object pre, Object self) {
super(name);
this.pre = pre;
this.self = self;
} public void run() {
for (int i = 0; i < 10; i++) {
synchronized (pre) {
synchronized (self) {
System.out.print(this.getName());
try {
self.notify();
} catch (Exception e) {
}
}
try {
pre.wait();
} catch (Exception e) {
}
}
}
}
}
public static void main(String args[]) {
Object a = new Object();// 创建三个object实例对象
Object b = new Object();
Object c = new Object(); Print pA = new Print("A", b, a);
Print pB = new Print("B", a, c);
Print pC = new Print("C", c, b);
pA.start();
pB.start();
pC.start();
}
}class Print extends Thread {
Object pre;
Object self; public Print(String name, Object pre, Object self) {// 构造方法
super(name);
this.pre = pre;
this.self = self;
} public void run() {
for (int i = 0; i < 10; i++) {
synchronized (pre) {
synchronized (self) {
System.out.print(this.getName());
try {
self.notify();
} catch (IllegalMonitorStateException e) {
e.printStackTrace();
}
}
try {
pre.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
private static Integer times=0;
/**
* @param args
*/
public static void main(String[] args) {
Thread A = new Thread(new Test2());
A.setName("线程A");
Thread B = new Thread(new Test2());
B.setName("线程B");
Thread C = new Thread(new Test2());
C.setName("线程C");
A.start();
B.start();
C.start();
} public void run() {
synchronized (times) {
while(times<11){
System.out.println("ABCABC");
times++;
}
}
}}
import java.util.concurrent.locks.LockSupport;public class Threads10 { static int counter = 0;
static LoopThread starter= null;
static boolean stopped = false;
static class LoopThread extends Thread {
LoopThread next = null;
LoopThread (String name) {
super(name);
}
void setNext(LoopThread next) {
this.next = next;
}
public void run() {
for(;;) {
if (starter == this){
// it is ok to use simple int here, thread safe!
counter++;
if (counter >= 10) stopped = true;
}
System.out.println(counter + ", Thread: " + getName());
if (next != null) {
if (!next.isAlive() && !stopped) next.start();
// we may need to spin this to avoid the thread starving...
// but the testing from my own PC, it works fine and won't make things complicated.
else LockSupport.unpark(next);
}
if ( stopped) {
return;
}
// park myself.
LockSupport.park();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
LoopThread threada = new LoopThread("A");
LoopThread threadb = new LoopThread("B");
LoopThread threadc = new LoopThread("C");
threada.setNext(threadb);
threadb.setNext(threadc);
threadc.setNext(threada);
starter = threada;
starter.start();
}}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package sample;import java.util.logging.Level;
import java.util.logging.Logger;/**
*
* @author bill
*/
public class NewClass {
int count=1;
public synchronized void print(aRunnable r){
while ((count % 3) != r.val) {
try {
this.wait();
} catch (InterruptedException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println(r.name);
count++;
notifyAll();
}
public synchronized int getCounting(){
return count;
} public static void main(String[] args) { NewClass b=new NewClass();
new Thread(new aRunnable(1, "A",b)).start();
new Thread(new aRunnable(2, "B",b)).start();
new Thread(new aRunnable(0, "c",b)).start(); }
static class aRunnable implements Runnable{
int val;
String name;
NewClass obj;
aRunnable(int selfVal,String name,NewClass obj){
this.val=selfVal;
this.name=name;
this.obj=obj;
}
@Override
public void run() {
while (obj.getCounting() < 20) {
obj.print(this);
}
}
@Override
public String toString() {
return name;
}
}
}好难!
package org.it315.test;import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class ThreadTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Business task = new Business();
new Thread(){
public void run() {
for(int i = 0; i < 10; i++) {
task.printA();
}
}
}.start();
new Thread(){
public void run() {
for(int i = 0; i < 10; i++) {
task.printB();
}
}
}.start();
new Thread(){
public void run() {
for(int i = 0; i < 10; i++) {
task.printC();
}
}
}.start();
}
private static class Business {
private int flag = 1;
private Lock lock = new ReentrantLock();
private Condition notA = lock.newCondition();
private Condition notB = lock.newCondition();
private Condition notC = lock.newCondition();
private void printA(){
try {
lock.lock();
while(flag != 1) {
notA.await();
}
System.out.print("A");
flag = 2;
notB.signal();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}
}
private void printB() {
try {
lock.lock();
while(flag != 2) {
notB.await();
}
System.out.print("B");
flag = 3;
notC.signal();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}
}
private void printC() {
try {
lock.lock();
while(flag != 3) {
notC.await();
}
System.out.println("C");
flag = 1;
notA.signal();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}
}
}}
/**
*打印控制类
*/
public class ContentHolder { private static String lastContent = null;
private static boolean canPrint(String threadContent){
if("A".equals(lastContent)){
if(threadContent.equals("B")){
return true;
}else{
return false;
}
}else if("B".equals(lastContent)){
if(threadContent.equals("C")){
return true;
}else{
return false;
}
}else if(lastContent == null ||"C".equals(lastContent)){
if(threadContent.equals("A")){
return true;
}else{
return false;
}
}
return false;
}
public static synchronized boolean print(String threadContent){
if(canPrint(threadContent)){
System.out.print(threadContent);
lastContent = threadContent;
return true;
}else{
return false;
}
}
}
/**
* 线程
*/
public class MyThread extends Thread { private String content = null;
public MyThread(String content){
this.content = content;
}
@Override
public void run() {
int index = 0;
while(index < 10){
if(ContentHolder.print(content)){
index++;
synchronized(MyThread.class){
MyThread.class.notifyAll();
}
}else{
synchronized(MyThread.class){
try {
MyThread.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
} /**
* 测试代码
*/
public static void main(String[] args) {
MyThread t1 = new MyThread("A");
MyThread t2 = new MyThread("B");
MyThread t3 = new MyThread("C");
t2.start();
t3.start();
t1.start();
}