我想创建一个简单的Android二进制时钟,但我的应用程序崩溃.我使用6个textview字段:3表示小数,3表示当前时间的二进制表示(HH:mm:ss).这是代码:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Binary extends Activity implements Runnable
{
Thread runner;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (runner == null)
{ //start the song
runner = new Thread(this);
runner.start();
}
}
@Override
public void run()
{
TextView hours_dec = (TextView) findViewById(R.id.hours_dec);
TextView mins_dec = (TextView) findViewById(R.id.mins_dec);
TextView secs_dec = (TextView) findViewById(R.id.secs_dec);
TextView hours_bin = (TextView) findViewById(R.id.hours_bin);
TextView mins_bin …Run Code Online (Sandbox Code Playgroud) InputStream in = ClientSocket.getInputStream();
new Thread()
{
public void run() {
while (true)
{
int i = in.read();
handleInput(i);
}
}
}.start();
Run Code Online (Sandbox Code Playgroud)
我正在使用此代码在套接字上侦听新数据并获取:
FaceNetChat.java:37: unreported exception java.io.IOException; must be caught or declared to be thrown
int i = in.read();
^
Run Code Online (Sandbox Code Playgroud)
当我在" run() " 之后添加" throws IOException "时,我得到:
FaceNetChat.java:34: run() in cannot implement run() in java.lang.Runnable; overridden method does not throw java.io.IOException
public void run() throws IOException {
^
Run Code Online (Sandbox Code Playgroud)
这可能很简单,但我很茫然.我怎么通过这个?
之前 ......(问题已删除)
我正在尝试从Swing项目中创建一个可运行的jar.
我在/ img文件夹中添加了一些图像.以前的版本没有它,并且在Eclipse中导出runnable jar很好.
现在我想有些事情出了问题.在runnable jar我添加了相同级别的主包和META-INF文件夹,这个img文件夹但似乎GUI没有出现.
构建GUI之前的一些过程进展顺利,因此主要类似乎没问题.
有什么建议!?
谢谢.
评论 :
在命令行上运行它并发布您获得的错误.- 凯文
有什么建议!?是.发布jar文件的结构,失败的代码,以及你得到的异常的堆栈跟踪.否则,我看不出我们如何能帮到你.- JB Nizet
你有没有把一些图像放到类加载器可以看到的路径中?- 感知
之后 ....
所以我发现了问题,仍然不是解决方案:
正如我所说,我通过第一个截图手动将/ img手动放入jar中:
http://img94.imageshack.us/img94/1691/screenshot021ep.jpg
但没有任何反应,现在我把/ img放在jar和GUI之外:
http://img21.imageshack.us/img21/1551/screenshot022jj.jpg
那么,我如何为我的应用程序打包/ img文件夹或图标?
谢谢.
PS:cmd行没有打印任何内容,应用程序启动并做了一些正确的事情,除了GUI构建.
我有一个java类
SomeClass implements Runnable
Run Code Online (Sandbox Code Playgroud)
其中有一个方法display().
当我创建这个类的线程时
Thread thread1 = new Thread(new SomeClass());
Run Code Online (Sandbox Code Playgroud)
现在我如何使用线程实例调用display()方法?
我正在使用Swing创建一个游戏.我制作start()并stop()同步,因为我被告知它更好.同步做什么以及使用它有什么好处?
我的代码:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
public class SpritePractice extends Canvas implements Runnable{
private JFrame frame;
private final static int WIDTH = 200, HEIGHT = 200;
private final static int SCALE = 2;
private final static Dimension dimens= new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
private BufferedImage image;
private Graphics g;
private long nanoSecond = 1000000000;
private double tick = nanoSecond/60;
private boolean running …Run Code Online (Sandbox Code Playgroud) 我是java的新手.我有两个类看起来像:
public class hsClient implements Runnable {
public void run() {
while(true){
}
}
}
public class hsServer implements Runnable {
public void run() {
while(true){
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试以Thread开始这两个类,它将不会启动第二个线程.看起来他陷入了第一个.
这是我的主要课程:
public static void main(String[] args) throws IOException {
hsClient client = new hsClient();
Thread tClient = new Thread(client);
tClient.run();
System.out.println("Start Client");
hsServer server = new hsServer();
Thread tServer = new Thread(server);
tServer.run();
System.out.println("Start Server");
}
Run Code Online (Sandbox Code Playgroud)
如果我运行我的代码,它只在控制台上打印"Start Client"而不是"Start Server"
我需要调试我的应用程序,并调试runnable内的代码,并在应用程序中执行一些操作然后再次调试代码,但我不能这样做,我不能做任何操作,调试点立即激活.
代码:
public class UpdateHandler {
public static Handler getHandler() {
if (sHandler == null) {
HandlerThread looper = new HandlerThread("update Handler");
looper.start();
sHandler = new Handler(looper.getLooper());
}
return sHandler;
}
}
UpdateHandler.getHandler().post(new Runnable() {
@Override
public void run() {
update();
}
});
public void update() {
// i put the debug point here .
}
Run Code Online (Sandbox Code Playgroud) 我试图使用以下代码在延迟后触发一个方法:
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new UpdateTDDBRejections(), 5, 10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
它应该等待延迟通过,触发"UpdateTDDBRejections"方法,该方法位于另一个Runnable类中,该方法将打印出一个短语然后再睡眠,然后再次打印短语等.
它不会这样做,它只是打印短语然后停止.
但是,当我这样设置时:
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable()
{
@Override
public void run() {
// do stuff
System.out.println("garbage garbage garbage");
}
}, 5, 10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
它按预期的ScheduledExecutorService运行.我的印象是,由于我使用Java 8.4,我不必添加TimerTask来按照我想要的方式进行此坐标.有什么建议?或者我根本无法使用ScheduledExecutorService调用其他Runnable类?
编辑:这是UpdateTDDBRejections类.
public class UpdateTDDBRejections implements Runnable
{
UpdateTDDBRejections()
{
System.out.println("garbage garbage garbage");
}
@Override
public void run()
{
}
}
Run Code Online (Sandbox Code Playgroud) 我想同时运行一些任务,所以我有一个代码如下:
for(final Task task : tasks){
(new Thread(){public void run(){
task.run(args);
}}).start();
Run Code Online (Sandbox Code Playgroud)
我怎么知道所有任务何时完成(任务量可以变化),以便我可以在完成所有任务后运行某些东西?
System.out.println("All tasks are finished");
Run Code Online (Sandbox Code Playgroud) 我目前正在与Eclipse Paho一起开发MQTT客户端服务,以使用更大的软件,并且遇到性能问题。我收到了很多要发布给代理的事件,并且正在使用GSON对这些事件进行序列化。我已经对序列化和发布进行了多线程处理。根据基本基准,序列化和发布最多需要1毫秒。我使用的ExecutorService的线程池大小为10(目前)。
我的代码当前每秒向ExecutorService提交大约50个Runnable,但是我的代理每秒仅报告5-10条消息。我之前已经对MQTT设置进行了基准测试,并设法以非多线程方式每秒发送约9000条以上的MQTT消息。
线程池是否有这么多的开销,我只能从中得到这么少的发布?
public class MqttService implements IMessagingService{
protected int PORT = 1883;
protected String HOST = "localhost";
protected final String SERVICENAME = "MQTT";
protected static final String COMMANDTOPIC = "commands";
protected static final String REMINDSPREFIX = "Reminds/";
protected static final String VIOLATIONTOPIC = "violations/";
protected static final String WILDCARDTOPIC = "Reminds/#";
protected static final String TCPPREFIX = "tcp://";
protected static final String SSLPREFIX = "ssl://";
private MqttClient client;
private MqttConnectOptions optionsPublisher = new MqttConnectOptions();
private ExecutorService pool = …Run Code Online (Sandbox Code Playgroud)