前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Java 中 Future 的 get 方法超时会怎样?

Java 中 Future 的 get 方法超时会怎样?

作者头像
明明如月学长
发布于 2022-06-15 04:59:26
发布于 2022-06-15 04:59:26
4.1K00
代码可运行
举报
运行总次数:0
代码可运行

一、背景

很多 Java 工程师在准备面试时,会刷很多八股文,线程和线程池这一块通常会准备线程的状态、线程的创建方式,Executors 里面的一些工厂方法和为什么不推荐使用这些工厂方法,ThreadPoolExecutor 构造方法的一些参数和执行过程等。

工作中,很多人会使用线程池的 submit 方法 获取 Future 类型的返回值,然后使用 java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit) 实现“最多等多久”的效果。

但很多人对此的理解只停留在表面上,稍微问深一点点可能就懵逼了。

比如,java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit) 超时之后,当前线程会怎样?线程池里执行对应任务的线程会有怎样的表现?

如果你对这个问题没有很大的把握,说明你掌握的还不够扎实。

最常见的理解就是,“超时以后,当前线程继续执行,线程池里的对应线程中断”,真的是这样吗?

二、模拟

2.1 常见写法

下面给出一个简单的模拟案例:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package basic.thread;

import java.util.concurrent.*;

public class FutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {

        ExecutorService executorService = Executors.newFixedThreadPool(2);

        Future<?> future = executorService.submit(() -> {
            try {
                demo();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + "获取的结果 -- start");
        Object result = future.get(100, TimeUnit.MILLISECONDS);
        System.out.println(threadName + "获取的结果 -- end :" + result);

    }

    private static String demo() throws InterruptedException {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + ",执行 demo -- start");
        TimeUnit.SECONDS.sleep(1);
        System.out.println(threadName + ",执行 demo -- end");
        return "test";
    }
}

输出结果:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
main获取的结果 -- start
pool-1-thread-1,执行 demo -- start
Exception in thread "main" java.util.concurrent.TimeoutException
	at java.util.concurrent.FutureTask.get(FutureTask.java:205)
	at basic.thread.FutureDemo.main(FutureDemo.java:20)
pool-1-thread-1,执行 demo -- end

我们可以发现:当前线程会因为收到 TimeoutException 而被中断,线程池里对应的线程“却”继续执行完毕。

2.2 尝试取消

我们尝试对未完成的线程进行取消,发现 Future#cancel 有个 boolean 类型的参数。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    /**
     * Attempts to cancel execution of this task.  This attempt will
     * fail if the task has already completed, has already been cancelled,
     * or could not be cancelled for some other reason. If successful,
     * and this task has not started when {@code cancel} is called,
     * this task should never run.  If the task has already started,
     * then the {@code mayInterruptIfRunning} parameter determines
     * whether the thread executing this task should be interrupted in
     * an attempt to stop the task.
     *
     * After this method returns, subsequent calls to {@link #isDone} will
     * always return {@code true}.  Subsequent calls to {@link #isCancelled}
     * will always return {@code true} if this method returned {@code true}.
     *
     * @param mayInterruptIfRunning {@code true} if the thread executing this
     * task should be interrupted; otherwise, in-progress tasks are allowed
     * to complete
     * @return {@code false} if the task could not be cancelled,
     * typically because it has already completed normally;
     * {@code true} otherwise
     */
    boolean cancel(boolean mayInterruptIfRunning);

看源码注释我们可以知道:

当设置为 true 时,正在执行的任务将被中断(interrupted);

当设置为 false 时,如果任务正在执行中,那么仍然允许任务执行完成。

2.2.1 cancel(false)

此时,为了不让主线程因为超时异常被中断,我们 try-catch 包起来。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package basic.thread;

import org.junit.platform.commons.util.ExceptionUtils;

import java.util.concurrent.*;

public class FutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {

        ExecutorService executorService = Executors.newFixedThreadPool(2);

        Future<?> future = executorService.submit(() -> {
            try {
                demo();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        String threadName = Thread.currentThread().getName();
        System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果 -- start");
        try {
            Object result = future.get(100, TimeUnit.MILLISECONDS);
            System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果 -- end :" + result);
        } catch (Exception e) {
            System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果异常:" + ExceptionUtils.readStackTrace(e));
        }
        future.cancel(false);
        System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果 -- cancel");
    }

    private static String demo() throws InterruptedException {
        String threadName = Thread.currentThread().getName();
        System.out.println(System.currentTimeMillis() + "," + threadName + ",执行 demo -- start");
        TimeUnit.SECONDS.sleep(1);
        System.out.println(System.currentTimeMillis() + "," + threadName + ",执行 demo -- end");
        return "test";
    }
}

结果:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1653751759233,main获取的结果 -- start
1653751759233,pool-1-thread-1,执行 demo -- start
1653751759343,main获取的结果异常:java.util.concurrent.TimeoutException
	at java.util.concurrent.FutureTask.get(FutureTask.java:205)
	at basic.thread.FutureDemo.main(FutureDemo.java:23)

1653751759351,main获取的结果 -- cancel
1653751760263,pool-1-thread-1,执行 demo -- end

我们发现,线程池里的对应线程在 cancel(false) 时,如果已经正在执行,则会继续执行完成。

2.2.2 cancel(true)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package basic.thread;

import org.junit.platform.commons.util.ExceptionUtils;

import java.util.concurrent.*;

public class FutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {

        ExecutorService executorService = Executors.newFixedThreadPool(2);

        Future<?> future = executorService.submit(() -> {
            try {
                demo();
            } catch (InterruptedException e) {
                System.out.println(System.currentTimeMillis() + "," + Thread.currentThread().getName() + ", Interrupted:" + ExceptionUtils.readStackTrace(e));
                throw new RuntimeException(e);
            }
        });

        String threadName = Thread.currentThread().getName();
        System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果 -- start");
        try {
            Object result = future.get(100, TimeUnit.MILLISECONDS);
            System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果 -- end :" + result);
        } catch (Exception e) {
            System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果异常:" + ExceptionUtils.readStackTrace(e));
        }
        future.cancel(true);
        System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果 -- cancel");
    }

    private static String demo() throws InterruptedException {
        String threadName = Thread.currentThread().getName();
        System.out.println(System.currentTimeMillis() + "," + threadName + ",执行 demo -- start");
        TimeUnit.SECONDS.sleep(1);
        System.out.println(System.currentTimeMillis() + "," + threadName + ",执行 demo -- end");
        return "test";
    }
}

执行结果:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1653752011246,main获取的结果 -- start
1653752011246,pool-1-thread-1,执行 demo -- start
1653752011347,main获取的结果异常:java.util.concurrent.TimeoutException
	at java.util.concurrent.FutureTask.get(FutureTask.java:205)
	at basic.thread.FutureDemo.main(FutureDemo.java:24)

1653752011363,pool-1-thread-1, Interrupted:java.lang.InterruptedException: sleep interrupted
	at java.lang.Thread.sleep(Native Method)
	at java.lang.Thread.sleep(Thread.java:340)
	at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
	at basic.thread.FutureDemo.demo(FutureDemo.java:36)
	at basic.thread.FutureDemo.lambda$main$0(FutureDemo.java:14)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

1653752011366,main获取的结果 -- cancel

可以看出,此时,如果目标线程未执行完,那么会收到 InterruptedException ,被中断。

当然,如果此时不希望目标线程被中断,可以使用 try-catch 包住,再执行其他逻辑。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package basic.thread;

import org.junit.platform.commons.util.ExceptionUtils;

import java.util.concurrent.*;

public class FutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {

        ExecutorService executorService = Executors.newFixedThreadPool(2);

        Future<?> future = executorService.submit(() -> {
            demo();
        });

        String threadName = Thread.currentThread().getName();
        System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果 -- start");
        try {
            Object result = future.get(100, TimeUnit.MILLISECONDS);
            System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果 -- end :" + result);
        } catch (Exception e) {
            System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果异常:" + ExceptionUtils.readStackTrace(e));
        }
        future.cancel(true);
        System.out.println(System.currentTimeMillis() + "," + threadName + "获取的结果 -- cancel");
    }

    private static String demo() {
        String threadName = Thread.currentThread().getName();
        System.out.println(System.currentTimeMillis() + "," + threadName + ",执行 demo -- start");
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            System.out.println(System.currentTimeMillis() + "," + threadName + ",执行 demo 被中断,自动降级");
        }
        System.out.println(System.currentTimeMillis() + "," + threadName + ",执行 demo -- end");
        return "test";
    }
}

执行结果:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1653752219803,main获取的结果 -- start
1653752219803,pool-1-thread-1,执行 demo -- start
1653752219908,main获取的结果异常:java.util.concurrent.TimeoutException
	at java.util.concurrent.FutureTask.get(FutureTask.java:205)
	at basic.thread.FutureDemo.main(FutureDemo.java:19)

1653752219913,main获取的结果 -- cancel
1653752219914,pool-1-thread-1,执行 demo 被中断,自动降级
1653752219914,pool-1-thread-1,执行 demo -- end

三、回归源码

我们直接看 java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit) 的源码注释,就可以清楚地知道各种情况的表现:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    /**
     * Waits if necessary for at most the given time for the computation
     * to complete, and then retrieves its result, if available.
     *
     * @param timeout the maximum time to wait
     * @param unit the time unit of the timeout argument
     * @return the computed result
     * @throws CancellationException if the computation was cancelled
     * @throws ExecutionException if the computation threw an
     * exception
     * @throws InterruptedException if the current thread was interrupted
     * while waiting
     * @throws TimeoutException if the wait timed out
     */
    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;

我们还可以选取几个常见的实现类,查看下实现的基本思路:

java.util.concurrent.FutureTask#get(long, java.util.concurrent.TimeUnit)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
   public V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException {
        if (unit == null)
            throw new NullPointerException();
        int s = state;
        if (s <= COMPLETING &&
            (s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)
            throw new TimeoutException();
        return report(s);
    }

java.util.concurrent.CompletableFuture#get(long, java.util.concurrent.TimeUnit)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    /**
     * Waits if necessary for at most the given time for this future
     * to complete, and then returns its result, if available.
     *
     * @param timeout the maximum time to wait
     * @param unit the time unit of the timeout argument
     * @return the result value
     * @throws CancellationException if this future was cancelled
     * @throws ExecutionException if this future completed exceptionally
     * @throws InterruptedException if the current thread was interrupted
     * while waiting
     * @throws TimeoutException if the wait timed out
     */
    public T get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException {
        Object r;
        long nanos = unit.toNanos(timeout);
        return reportGet((r = result) == null ? timedGet(nanos) : r);
    }
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  /**
     * Returns raw result after waiting, or null if interrupted, or
     * throws TimeoutException on timeout.
     */
    private Object timedGet(long nanos) throws TimeoutException {
        if (Thread.interrupted())
            return null;
        if (nanos <= 0L)
            throw new TimeoutException();
        long d = System.nanoTime() + nanos;
        Signaller q = new Signaller(true, nanos, d == 0L ? 1L : d); // avoid 0
        boolean queued = false;
        Object r;
        // We intentionally don't spin here (as waitingGet does) because
        // the call to nanoTime() above acts much like a spin.
        while ((r = result) == null) {
            if (!queued)
                queued = tryPushStack(q);
            else if (q.interruptControl < 0 || q.nanos <= 0L) {
                q.thread = null;
                cleanStack();
                if (q.interruptControl < 0)
                    return null;
                throw new TimeoutException();
            }
            else if (q.thread != null && result == null) {
                try {
                    ForkJoinPool.managedBlock(q);
                } catch (InterruptedException ie) {
                    q.interruptControl = -1;
                }
            }
        }
        if (q.interruptControl < 0)
            r = null;
        q.thread = null;
        postComplete();
        return r;
    }

java.util.concurrent.Future#cancel 也一样

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * Attempts to cancel execution of this task.  This attempt will
 * fail if the task has already completed, has already been cancelled,
 * or could not be cancelled for some other reason. If successful,
 * and this task has not started when {@code cancel} is called,
 * this task should never run.  If the task has already started,
 * then the {@code mayInterruptIfRunning} parameter determines
 * whether the thread executing this task should be interrupted in
 * an attempt to stop the task.
 *
 * After this method returns, subsequent calls to {@link #isDone} will
 * always return {@code true}.  Subsequent calls to {@link #isCancelled}
 * will always return {@code true} if this method returned {@code true}.
 *
 * @param mayInterruptIfRunning {@code true} if the thread executing this
 * task should be interrupted; otherwise, in-progress tasks are allowed
 * to complete
 * @return {@code false} if the task could not be cancelled,
 * typically because it has already completed normally;
 * {@code true} otherwise
 */
boolean cancel(boolean mayInterruptIfRunning);

java.util.concurrent.FutureTask#cancel

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public boolean cancel(boolean mayInterruptIfRunning) {
        if (!(state == NEW &&
              UNSAFE.compareAndSwapInt(this, stateOffset, NEW,
                  mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
            return false;
        try {    // in case call to interrupt throws exception
            if (mayInterruptIfRunning) {
                try {
                    Thread t = runner;
                    if (t != null)
                        t.interrupt();
                } finally { // final state
                    UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);
                }
            }
        } finally {
            finishCompletion();
        }
        return true;
    }

可以看到 mayInterruptIfRunning 为 true 时,会执行 Thread#interrupt 方法

java.util.concurrent.CompletableFuture#cancel

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    /**
     * If not already completed, completes this CompletableFuture with
     * a {@link CancellationException}. Dependent CompletableFutures
     * that have not already completed will also complete
     * exceptionally, with a {@link CompletionException} caused by
     * this {@code CancellationException}.
     *
     * @param mayInterruptIfRunning this value has no effect in this
     * implementation because interrupts are not used to control
     * processing.
     *
     * @return {@code true} if this task is now cancelled
     */
    public boolean cancel(boolean mayInterruptIfRunning) {
        boolean cancelled = (result == null) &&
            internalComplete(new AltResult(new CancellationException()));
        postComplete();
        return cancelled || isCancelled();
    }

通过注释我们也发现,不同的实现类对参数的“效果”也有差异。

四、总结

我们学习时不应该想当然,不能纸上谈兵,对于不太理解的地方,可以多看源码注释,多看源码,多写 DEMO 去模拟或调试。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022/06/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
你从未见过的EditText属性详解
Hi,小伙伴们, Layout学会了, Button和 TextView学会了, ImageView也学会了,是不是感觉总是学习这些单一的东西稍微有点枯燥了呢?那么学习了这篇文章之后,开始尽情发挥你们的想象力开始搞事情吧~
下码看花
2019/09/02
3.3K0
你从未见过的EditText属性详解
EditText 集锦 - 开发中常用的用法及遇到的各种坑
EditText ,文本输入框,一个再熟悉不过的的控件,在开发当中,我们需要经常用到。这边文章,主要是记录 EditText 的常用用法,需要的时候可以直接复制张贴,提高效率。同时,本文章会持续更新,建议收藏起来。
程序员徐公
2019/03/04
2.3K0
EditText的属性和使用方法
EditText与TextView非常相似,它甚至与TextView 共用了绝大部分XML属性和方法。EditText与TextView的最大区别在于:EditText可以接受用户输入。 一、EditText简介 EditText支持的XML属性及相关方法见TextView表中介绍的与输入有关的属性和方法,其中比较重要的一个属性是inputType,用于为EditText设置输入类型,其属性值主要有以下一些。 n android:inputType="none":普通字符。 n android:input
分享达人秀
2018/02/02
2.6K0
EditText的属性和使用方法
Android EditText使用详解-包含很多教程上看不到的功能演示
标题有点大,说是详解,其实就是对EditText的一些常用功能的介绍,包括密码框,电话框,空白提示文字等等的讲解,尽量的介绍详细一点,也就是所谓的详解了。。呵呵
飞雪无情
2018/08/28
4K0
Android EditText使用详解-包含很多教程上看不到的功能演示
常用控件之TextView全解析
大家好!在前几篇文章里,我们详细介绍了Android中的常用布局,使大家对Android中的页面布局有了一定认识,而对于布局中使用的一些UI控件如Button、TextView等,有的读者可能还存在一些困惑。在接下来文章中,我们将详细介绍Android开发中经常使用的UI控件,敬请期待!
下码看花
2019/09/02
2.3K0
常用控件之TextView全解析
Android UI学习之EditText
比如上面那正图片就是我截取的添加联系人的界面,很明显能输入文本的就是EditText。
DragonKingZhu
2022/05/08
4710
Android UI学习之EditText
【Flutter 专题】64 图解基本 TextField 文本输入框 (一)
和尚最近在学习基础的 Flutter Widget,原因在于很多基础的组件有很多容易忽视的注意事项,了解并熟悉后对整体的开发认知会有所提升;今天和尚学习一下 TextField 文本输入框;
阿策小和尚
2019/10/22
4.7K0
Android开发笔记(三十六)展示类控件
View是单个视图,所有的控件类都是从它派生出来;而ViewGroup是个视图组织,所有的布局视图类都是从它派生出来。由于View和ViewGroup是基类,因此很少会直接使用,偶尔用到的场景,主要有如下几个: 1、页面上需要单独显示一条横线或者竖线。如果填充图片显然不够经济,最简单的做法,就是在xml布局中增加一个View控件,高度或宽度设置为1dp,背景颜色设置为线条颜色,这样便实现了单独显示线条的需求。 2、点击事件的处理函数onClick(View v),这里面我们要调用View的getId方法获取发生点击事件的控件id,从而进行该控件对应的点击处理。 3、在代码中设置某控件为可见或不可见或消失,此时需要使用View类的三个变量,分别是View.VISIBLE、View.INVISIBLE和View.GONE。
aqi00
2019/01/18
1.5K0
浅谈EditText控件的inputType类型
android:inputType="none"--默认 android:inputType="text"--输入文本字符 android:inputType="textCapCharacters"--字母大写 android:inputType="textCapWords"--单词首字母大小 android:inputType="textCapSentences"--仅第一个字母大小 android:inputType="textAutoCorrect"--自动更正 android:i
听着music睡
2018/06/08
1.8K0
11/19Android开发笔记—EditTex多行输入及相关问题
从今天起可以传最近的了,虽然依旧会有些延迟O(∩_∩)O~。由于直接在真机上运行了,相关图片只能回头用虚拟机单独截了。
WindCoder
2018/09/20
8680
11/19Android开发笔记—EditTex多行输入及相关问题
TextView属性和方法大全
前面简单学习了一些Android UI的一些基础知识,那么接下来我们一起来详细学习Android的UI界面基本组件。 一、认识TextView 我们知道前面学习的HelloWorld应用程序中就是使用
分享达人秀
2018/02/02
2.1K0
TextView属性和方法大全
[android] 短信发送器
/*****************2016年4月23日 更新********************************/
唯一Chat
2019/09/10
4.4K0
超全的Android组件及UI框架
设计和代码切换,一般情况下,我们 UI 布局都是先拖再细调整,也就是先用设计默认拖出一个大概的布局,然后用代码来微调
红目香薰
2022/11/29
6.3K0
超全的Android组件及UI框架
自定义键盘(二)[通俗易懂]
上一篇文章只是自定义了一个键盘的样式,并未和任何的输入框进行关联。只有和输入框进行关联才能是一个有用的键盘。不知道你有没有注意到应用市场上有这样一类app:第三方输入法app,比如讯飞输入法,搜狗输入法;
全栈程序员站长
2022/08/04
9870
Flutter TextField详解
TextField是一个material design风格的输入框,本身有多种属性,除此之外装饰器InputDecoration也有多种属性,但都比较简单,所以不必担心,且听我娓娓道来。
yechaoa
2022/06/10
4.3K0
Flutter TextField详解
【Flutter实战】文本组件及五大案例
老孟导读:大家好,这是【Flutter实战】系列文章的第二篇,这一篇讲解文本组件,文本组件包括文本展示组件(Text和RichText)和文本输入组件(TextField),基础用法和五个案例助你快速掌握。
老孟Flutter
2020/09/11
7.3K0
【Flutter实战】文本组件及五大案例
SearchView使用详解
搜索在一般APP中是基本功能,且非常重要。 常见的有组装的EditText,今天主要讲的是SearchView。
yechaoa
2022/06/10
1.2K0
SearchView使用详解
study - 一文入门正则表达式
如图所示的正则,将日期和时间都括号括起来。这个正则中一共有两个分组,日期是第 1 个,时间是第 2 个。
stark张宇
2023/03/04
5740
Flutter组件学习(三)—— 输入框TextFiled
Google 前两天发布了 Flutter 1.0 正式版本,正式版发布之后,LZ身边越来越多的人都开始入坑了,不得不说 Flutter 框架的魅力还是很吸引人的哈,所以我们更要抓紧学习了;之前我写了两篇文章来介绍 Flutter中的Text组件 和 Flutter中的Image组件,今天我们继续学习输入框 TextFiled 组件,话不多说,先上图:
用户2802329
2018/12/26
2.6K0
React Native控件只TextInput
TextInput是一个允许用户在应用中通过键盘输入文本的基本组件。本组件的属性提供了多种特性的配置,譬如自动完成、自动大小写、占位文字,以及多种不同的键盘类型(如纯数字键盘)等等。 比如官网最简单的写法: import React, { Component } from 'react'; import { AppRegistry, TextInput } from 'react-native'; class UselessTextInput extends Component { construct
xiangzhihong
2018/02/05
3.7K0
React Native控件只TextInput
推荐阅读
相关推荐
你从未见过的EditText属性详解
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文