`
ruantongsheng
  • 浏览: 21145 次
  • 来自: ...
社区版块
存档分类
最新评论

suan fa

阅读更多
1.==============================ABC3Threads============================
package test;


public class ABC3Threads {

static boolean taStarted=false;
static boolean tbStarted=false;
static boolean tcStarted=false;
public static void main(String[] args) {
try {

int i=0;
while(i++<10){
taStarted=false;
tbStarted=false;
tcStarted=false;
Object oA=new Object();
Object oB=new Object();
Object oC=new Object();
OutputThread ta=new OutputThread(oC, oA,"A");
OutputThread tb=new OutputThread(oA, oB,"B");
OutputThread tc=new OutputThread(oB, oC,"C");

ta.start();
// Thread.sleep(3);
while(!taStarted){
Thread.sleep(1);
}
tb.start();
while(!tbStarted){
Thread.sleep(1);
}
//Thread.sleep(3);
tc.start();
//Thread.sleep(1000);
while(ta.isAlive()){
Thread.sleep(1);

}

System.out.println("");
// Thread.sleep(3111);
// ta.interrupt();
// tb.interrupt();
// tc.interrupt();
ta.stop();
tb.stop();
tc.stop();
}
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class OutputThread extends Thread{
Object obj1;
Object obj2;
String name;
int i=10;
public OutputThread(Object o1,Object o2,String name) {
obj1=o1;
obj2=o2;
this.name=name;
}
public void run() {
while(i-->0){
//System.out.println("i===================="+i);
synchronized (obj1) {
synchronized (obj2) {
try {
System.out.print(name);
if(name.equals("A")&&i==9){
ABC3Threads.taStarted=true;
}
if(name.equals("B")&&i==9){
ABC3Threads.tbStarted=true;
}
if(name.equals("C")&&i==9){
ABC3Threads.tcStarted=true;
}
obj2.notify();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
obj1.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}

}
}

2.==============================ABCD4Threads============================
package test;

public class ABCD4Threads {

public static void main(String[] args) {
try {
Object a=new Object();
Object b=new Object();
Object c=new Object();
Object d=new Object();
ABCD4T ta=new ABCD4T(d, a, "A");
ABCD4T tb=new ABCD4T(a, b, "B");
ABCD4T tc=new ABCD4T(b, c, "C");
ABCD4T td=new ABCD4T(c, d, "D");

ta.start();
Thread.sleep(50);
tb.start();
Thread.sleep(50);
tc.start();
Thread.sleep(50);
td.start();
//System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}

class ABCD4T extends Thread {
Object p1;
Object c;
String name;

public ABCD4T(Object p1,Object c, String name) {
super(name);
this.p1 = p1;
this.c = c;
this.name = name;
}
int i=10;
public void run() {
try {
while(i-->0){
String namemmm=this.getName();
synchronized (p1) {
synchronized (c) {
System.out.print(name);
c.notify();
}
p1.wait();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

3.==============================PutOneGetOne============================
package test;

public class PutOneGetOne {

String objg ;
public static void main(String[] args) {
try {
quangKu s=new quangKu();
F ft=new F(s);
Q qt=new Q(s);
s.setFQ(ft, qt);
ft.start();
Thread.sleep(10);
qt.start();

} catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
}
class quangKu{
F f;
Q q;
public void setFQ(F f,Q q) {
this.f=f;
this.q=q;
}
String objg ;
public  void put(String obj){
try {
synchronized (f) {
synchronized (q) {
objg=obj;
System.out.println("put:"+objg);
q.notify();
}
f.wait();
}

} catch (Exception e) {
e.printStackTrace();
}
}
public  void get(){
try {
synchronized (q) {
synchronized (f) {

System.out.println("get:"+objg);
f.notify();
}
q.wait();

}
} catch (Exception e) {
e.printStackTrace();
}

}
}
class F extends Thread{
quangKu s;
public F(quangKu s) {
this.s=s;

}
int i=10;
public void run() {

try {
while(i-->0){

s.put(i+"");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Q extends Thread{

quangKu s;
public Q(quangKu s) {
this.s=s;
}
int i=10;
public void run() {
while(i-->0){
s.get();
}
}
}

4.==============================JiCheng============================
package test;

public class JiCheng {

public static void main(String[] args) {
try {
System.out.println(jicheng(100000));
} catch (Exception e) {
e.printStackTrace();
}
}
private static String jicheng(int n)throws Exception{
int weiShu=1;
//System.out.println(Integer.MAX_VALUE);
int []result=new int[21474836];
result[0]=1;
while(n>1){
int jinWei=0;
for(int i=0;i<weiShu;i++){
int temp=result[i]*n+jinWei;
int geWei=temp%10;
jinWei=temp/10;
result[i]=geWei;

}
//==process jinWei
while(jinWei>0){
int nextGeWei=jinWei%10;
result[weiShu++]=nextGeWei;
jinWei/=10;
}
n--;
}
//=======================process result
String res="";
for(int i=weiShu-1;i>=0;i--){
res+=result[i];
}
return res;
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics