我有一个关于Java 8的问题Runnable.
public static void main(String[] args) {
Runnable r1 = Test::t1;
Runnable r2 = Test::t2;
Runnable r3 = Test::t3;
}
public static void t1() {
}
public static String t2() {
return "abc";
}
public static String t3(String t) {
return t;
}
Run Code Online (Sandbox Code Playgroud)
正如代码所示,我理解r1是对的,r3也是错的,但我不明白为什么r2也是对的.任何人都可以帮我理解吗?
我是Java的线程编程的新手,因此这个基本问题.(我检查过,但以前找不到这个问题)
我读到可以通过继承Thread类或通过实现Runnable接口来创建线程.我看到一个代码同时出现在同一个类中.
public class ThreadExample extends Thread implements Runnable {
}
Run Code Online (Sandbox Code Playgroud)
我想知道这种情况会是什么样的,如果有什么优势,那是什么呢.
如果我有一个变量int x = 1,比如说,我在主线程中声明了一个runnable,并且我想将x传递给runnable的run()方法,那么必须声明它final.为什么?
final int x = 0;//<----must be final...
private class myRun implements Runnable {
@Override
public void run() {
x++;//
}
}
Run Code Online (Sandbox Code Playgroud) 在stackoverflow上,我经常看到使用Thread.currentThread().isInterrupted().Runnable在while循环中实现和使用它时,如下所示:
public void run() {
while(!Thread.currentThread().isInterrupted()) { ... }
}
Run Code Online (Sandbox Code Playgroud)
是否有任何不同使用Thread.interrupted()(除了interrupted使用时清除标志interrupted())?
我也见过Thread.currentThread().interrupted().这是正确的使用方式,还是Thread.interrupted()足够的?
Runnable用作回调被认为是不好的做法吗?
考虑到这Runnable是用于线程(请参阅它的JavaDoc),我想知道这是否可以 - 或者我是否应该为此目的创建自己的接口.
我所说的是这样的:
public class KeyBinding {
public KeyBinding(KeyStroke stroke, Runnable handler) {
//...
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试建立一个可以每隔5秒加载一次广告的可运行(当然5秒太快,它仅用于测试目的)
这是我的代码:
package com.admobsdk_dfp_handler;
import com.google.ads.*;
import com.google.ads.doubleclick.*;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.RelativeLayout;
public class AdMobSDK_DFP_Handler extends Activity {
private DfpAdView adView;
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {
adView.loadAd(new AdRequest());
handler.postDelayed(this, 5000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad_mob_sdk__dfp__handler);
adView = new DfpAdView(
this,
AdSize.BANNER,
AD_UNIT_ID);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
layout.addView(adView);
adView.loadAd(new AdRequest());
handler.postDelayed(runnable, 5000);
};
@Override
protected void onDestroy() …Run Code Online (Sandbox Code Playgroud) 作为初学者,我正在阅读有关在Java中实现多线程的两种方法.
我在SO和许多其他线程上阅读了这个帖子.
据说这是
"prefer runnable",仅在您专门处理Thread的行为时才扩展线程.
有人可以通过为我提供一小段片段来帮助我理解这一行,从而解释我对Thread行为的专业意义.
当用户有很多可以接受某种意图的应用程序时,intentchooser需要一段时间才能加载,至少需要花费大量时间
然而,这种等待发生在我启动意图之后,有没有什么方法可以设置某种加载指示器或中间视觉元素来让用户不太注意时间?
直到现在我设法找到我需要的所有答案,但这个令我困惑.假设我们有示例代码:
public class Animal {
private String species;
private boolean canHop;
private boolean canSwim;
public Animal(String speciesName, boolean hopper, boolean swimmer) {
species = speciesName;
canHop = hopper;
canSwim = swimmer;
}
public boolean canHop() { return canHop; }
public boolean canSwim() { return canSwim; }
public String toString() { return species; }
}
public interface CheckAnimal {
public boolean test(Animal a);
}
public class FindSameAnimals {
private static void print(Animal animal, CheckAnimal trait) {
if(trait.test(animal)){
System.out.println(animal);
}
public static …Run Code Online (Sandbox Code Playgroud) 我是科特林初学者。我尝试创建一个每 2 秒重复一次的任务。所以我创造了这样的东西。
val handler = Handler()
handler.postDelayed(Runnable {
// TODO - Here is my logic
// Repeat again after 2 seconds
handler.postDelayed(this, 2000)
}, 2000)
Run Code Online (Sandbox Code Playgroud)
但在 postDelayed(this) 中它给出了错误 - required Runnable!, found MainActivity。我什至尝试过,this@Runnable但没有成功。
但是当我像这样编写相同的函数时,它就可以工作了
val handler = Handler()
handler.postDelayed(object : Runnable {
override fun run() {
// TODO - Here is my logic
// Repeat again after 2 seconds
handler.postDelayed(this, 2000)
}
}, 2000)
Run Code Online (Sandbox Code Playgroud)
那么为什么this关键字在第一个函数中不起作用,但在第二个函数中却很好呢?