当前位置: 技术问答>java相关
能举个最简单的多线程同步的程序例子吗?
来源: 互联网 发布时间:2015-02-02
本文导语: | 一个时钟: import java.awt.Graphics; import java.awt.Color; import java.applet.Applet; import java.text.*; import java.util.*; public class ClockDemo extends Applet implements Runnable { SimpleDateFormat formatter; // ノㄓ陪ボ丁妓Α ...
|
一个时钟:
import java.awt.Graphics;
import java.awt.Color;
import java.applet.Applet;
import java.text.*;
import java.util.*;
public class ClockDemo extends Applet implements Runnable {
SimpleDateFormat formatter; // ノㄓ陪ボ丁妓Α
Date currentDate; // 瞷丁
Thread clockThread = null;
boolean keepRunning = true;
public void init() {
int x,y;
formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
currentDate = new Date();
}
public void start() {
if(clockThread == null) {
clockThread = new Thread(this);
clockThread.start();
}
}
public void run() {
while(keepRunning) {
repaint();
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
}
}
// ノよ猭stopㄓ璶―磅︽氨ゎ磅︽
public void pleaseStop() { keepRunning = false; }
public void paint(Graphics g) {
int s, m, h;
String today;
currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
try {
s = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
s = 0;
}
formatter.applyPattern("m");
try {
m = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
m = 0;
}
formatter.applyPattern("h");
try {
h = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
h = 0;
}
formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
today = formatter.format(currentDate);
Clock myClock = new Clock(h, m, s);
g.drawString(today, 0, 160);
myClock.show(g, 70, 70, 70);
}
}
class Clock {
Clock(int hrs, int min, int sec) {
hour = hrs % 12;
minute = min;
second = sec;
}
void show(Graphics g, int center_x, int center_y, int radius) {
int hrs_nid_len = (int)(radius * 0.5), // ﹚皐
min_nid_len = (int)(radius * 0.7), // ﹚だ皐
sec_nid_len = (int)(radius * 0.85); // ﹚皐
double theta;
// 礶牧
g.drawOval(center_x - radius, center_y - radius,
radius * 2, radius * 2);
g.drawString("9",0,center_y);
g.drawString("3",center_x*2-10,center_y);
g.drawString("12",center_x,10);
g.drawString("6",center_x,center_y*2);
// 礶皐
theta = (double)(hour * 60 * 60 + minute * 60 + second) /
43200.0 * 2.0 * Math.PI;
drawNiddle(g, Color.blue, center_x, center_y, hrs_nid_len, theta);
// 礶だ皐
theta = (double)(minute * 60 + second) /
3600.0 * 2.0 * Math.PI;
drawNiddle(g, Color.blue, center_x, center_y, min_nid_len, theta);
// 礶皐
theta = (double)second / 60.0 * 2.0 * Math.PI;
drawNiddle(g, Color.red, center_x, center_y, sec_nid_len, theta);
}
private void drawNiddle(Graphics g, Color c, int x, int y,
int len, double theta) {
g.setColor(c);
g.drawLine(x, y, (int)(x + len * Math.sin(theta)),
(int)(y - len * Math.cos(theta)));
}
int hour, minute, second;
}
import java.awt.Graphics;
import java.awt.Color;
import java.applet.Applet;
import java.text.*;
import java.util.*;
public class ClockDemo extends Applet implements Runnable {
SimpleDateFormat formatter; // ノㄓ陪ボ丁妓Α
Date currentDate; // 瞷丁
Thread clockThread = null;
boolean keepRunning = true;
public void init() {
int x,y;
formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
currentDate = new Date();
}
public void start() {
if(clockThread == null) {
clockThread = new Thread(this);
clockThread.start();
}
}
public void run() {
while(keepRunning) {
repaint();
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
}
}
// ノよ猭stopㄓ璶―磅︽氨ゎ磅︽
public void pleaseStop() { keepRunning = false; }
public void paint(Graphics g) {
int s, m, h;
String today;
currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
try {
s = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
s = 0;
}
formatter.applyPattern("m");
try {
m = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
m = 0;
}
formatter.applyPattern("h");
try {
h = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
h = 0;
}
formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
today = formatter.format(currentDate);
Clock myClock = new Clock(h, m, s);
g.drawString(today, 0, 160);
myClock.show(g, 70, 70, 70);
}
}
class Clock {
Clock(int hrs, int min, int sec) {
hour = hrs % 12;
minute = min;
second = sec;
}
void show(Graphics g, int center_x, int center_y, int radius) {
int hrs_nid_len = (int)(radius * 0.5), // ﹚皐
min_nid_len = (int)(radius * 0.7), // ﹚だ皐
sec_nid_len = (int)(radius * 0.85); // ﹚皐
double theta;
// 礶牧
g.drawOval(center_x - radius, center_y - radius,
radius * 2, radius * 2);
g.drawString("9",0,center_y);
g.drawString("3",center_x*2-10,center_y);
g.drawString("12",center_x,10);
g.drawString("6",center_x,center_y*2);
// 礶皐
theta = (double)(hour * 60 * 60 + minute * 60 + second) /
43200.0 * 2.0 * Math.PI;
drawNiddle(g, Color.blue, center_x, center_y, hrs_nid_len, theta);
// 礶だ皐
theta = (double)(minute * 60 + second) /
3600.0 * 2.0 * Math.PI;
drawNiddle(g, Color.blue, center_x, center_y, min_nid_len, theta);
// 礶皐
theta = (double)second / 60.0 * 2.0 * Math.PI;
drawNiddle(g, Color.red, center_x, center_y, sec_nid_len, theta);
}
private void drawNiddle(Graphics g, Color c, int x, int y,
int len, double theta) {
g.setColor(c);
g.drawLine(x, y, (int)(x + len * Math.sin(theta)),
(int)(y - len * Math.cos(theta)));
}
int hour, minute, second;
}